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);
333 *--------------------------------------------------------------
336 * Free a event structure obtained from gdk_event_copy. Do not use
337 * with other event structures.
340 * "event" is the event struct to free.
345 * The reference count of the window in the event is decreased and
346 * might be freed, too.
348 *-------------------------------------------------------------- */
351 gdk_event_free (GdkEvent *event)
353 g_return_if_fail (event != NULL);
355 g_assert (event_chunk != NULL); /* paranoid */
357 if (event->any.window)
358 gdk_window_unref (event->any.window);
360 switch (event->any.type)
363 case GDK_KEY_RELEASE:
364 g_free (event->key.string);
367 case GDK_ENTER_NOTIFY:
368 case GDK_LEAVE_NOTIFY:
369 if (event->crossing.subwindow != NULL)
370 gdk_window_unref (event->crossing.subwindow);
375 case GDK_DRAG_MOTION:
376 case GDK_DRAG_STATUS:
378 case GDK_DROP_FINISHED:
379 gdk_drag_context_unref (event->dnd.context);
382 case GDK_BUTTON_PRESS:
383 case GDK_BUTTON_RELEASE:
384 if (event->button.axes)
385 g_free (event->button.axes);
388 case GDK_MOTION_NOTIFY:
389 if (event->motion.axes)
390 g_free (event->motion.axes);
397 g_mem_chunk_free (event_chunk, event);
401 * gdk_event_get_time:
402 * @event: a #GdkEvent
404 * Returns the time stamp from @event, if there is one; otherwise
405 * returns #GDK_CURRENT_TIME. If @event is %NULL, returns #GDK_CURRENT_TIME.
407 * Return value: time stamp field from @event
410 gdk_event_get_time (GdkEvent *event)
415 case GDK_MOTION_NOTIFY:
416 return event->motion.time;
417 case GDK_BUTTON_PRESS:
418 case GDK_2BUTTON_PRESS:
419 case GDK_3BUTTON_PRESS:
420 case GDK_BUTTON_RELEASE:
421 return event->button.time;
423 return event->scroll.time;
425 case GDK_KEY_RELEASE:
426 return event->key.time;
427 case GDK_ENTER_NOTIFY:
428 case GDK_LEAVE_NOTIFY:
429 return event->crossing.time;
430 case GDK_PROPERTY_NOTIFY:
431 return event->property.time;
432 case GDK_SELECTION_CLEAR:
433 case GDK_SELECTION_REQUEST:
434 case GDK_SELECTION_NOTIFY:
435 return event->selection.time;
436 case GDK_PROXIMITY_IN:
437 case GDK_PROXIMITY_OUT:
438 return event->proximity.time;
441 case GDK_DRAG_MOTION:
442 case GDK_DRAG_STATUS:
444 case GDK_DROP_FINISHED:
445 return event->dnd.time;
446 case GDK_CLIENT_EVENT:
447 case GDK_VISIBILITY_NOTIFY:
450 case GDK_FOCUS_CHANGE:
457 /* return current time */
461 return GDK_CURRENT_TIME;
465 * gdk_event_get_state:
466 * @event: a #GdkEvent or NULL
467 * @state: return location for state
469 * If the event contains a "state" field, puts that field in @state. Otherwise
470 * stores an empty state (0). Returns %TRUE if there was a state field
471 * in the event. @event may be %NULL, in which case it's treated
472 * as if the event had no state field.
474 * Return value: %TRUE if there was a state field in the event
477 gdk_event_get_state (GdkEvent *event,
478 GdkModifierType *state)
480 g_return_val_if_fail (state != NULL, FALSE);
485 case GDK_MOTION_NOTIFY:
486 *state = event->motion.state;
488 case GDK_BUTTON_PRESS:
489 case GDK_2BUTTON_PRESS:
490 case GDK_3BUTTON_PRESS:
491 case GDK_BUTTON_RELEASE:
492 *state = event->button.state;
495 *state = event->scroll.state;
498 case GDK_KEY_RELEASE:
499 *state = event->key.state;
501 case GDK_ENTER_NOTIFY:
502 case GDK_LEAVE_NOTIFY:
503 *state = event->crossing.state;
505 case GDK_PROPERTY_NOTIFY:
506 *state = event->property.state;
508 case GDK_VISIBILITY_NOTIFY:
509 *state = event->visibility.state;
511 case GDK_CLIENT_EVENT:
514 case GDK_FOCUS_CHANGE:
515 case GDK_SELECTION_CLEAR:
516 case GDK_SELECTION_REQUEST:
517 case GDK_SELECTION_NOTIFY:
518 case GDK_PROXIMITY_IN:
519 case GDK_PROXIMITY_OUT:
522 case GDK_DRAG_MOTION:
523 case GDK_DRAG_STATUS:
525 case GDK_DROP_FINISHED:
541 * gdk_event_get_axis:
542 * @event: a #GdkEvent
543 * @axis_use: the axis use to look for
544 * @value: location to store the value found
546 * Extract the axis value for a particular axis use from
547 * an event structure.
549 * Return value: %TRUE if the specified axis was found, otherwise %FALSE
552 gdk_event_get_axis (GdkEvent *event,
559 g_return_val_if_fail (event != NULL, FALSE);
561 if (axis_use == GDK_AXIS_X || axis_use == GDK_AXIS_Y)
567 case GDK_MOTION_NOTIFY:
575 case GDK_BUTTON_PRESS:
576 case GDK_BUTTON_RELEASE:
580 case GDK_ENTER_NOTIFY:
581 case GDK_LEAVE_NOTIFY:
582 x = event->crossing.x;
583 y = event->crossing.y;
590 if (axis_use == GDK_AXIS_X && value)
592 if (axis_use == GDK_AXIS_Y && value)
597 else if (event->type == GDK_BUTTON_PRESS ||
598 event->type == GDK_BUTTON_RELEASE)
600 device = event->button.device;
601 axes = event->button.axes;
603 else if (event->type == GDK_MOTION_NOTIFY)
605 device = event->motion.device;
606 axes = event->motion.axes;
611 return gdk_device_get_axis (device, axes, axis_use, value);
615 *--------------------------------------------------------------
616 * gdk_set_show_events
618 * Turns on/off the showing of events.
621 * "show_events" is a boolean describing whether or
622 * not to show the events gdk receives.
627 * When "show_events" is TRUE, calls to "gdk_event_get"
628 * will output debugging informatin regarding the event
629 * received to stdout.
631 *--------------------------------------------------------------
635 gdk_set_show_events (gboolean show_events)
638 gdk_debug_flags |= GDK_DEBUG_EVENTS;
640 gdk_debug_flags &= ~GDK_DEBUG_EVENTS;
644 gdk_get_show_events (void)
646 return (gdk_debug_flags & GDK_DEBUG_EVENTS) != 0;
650 gdk_io_destroy (gpointer data)
652 GdkIOClosure *closure = data;
655 closure->notify (closure->data);
660 /* What do we do with G_IO_NVAL?
662 #define READ_CONDITION (G_IO_IN | G_IO_HUP | G_IO_ERR)
663 #define WRITE_CONDITION (G_IO_OUT | G_IO_ERR)
664 #define EXCEPTION_CONDITION (G_IO_PRI)
667 gdk_io_invoke (GIOChannel *source,
668 GIOCondition condition,
671 GdkIOClosure *closure = data;
672 GdkInputCondition gdk_cond = 0;
674 if (condition & READ_CONDITION)
675 gdk_cond |= GDK_INPUT_READ;
676 if (condition & WRITE_CONDITION)
677 gdk_cond |= GDK_INPUT_WRITE;
678 if (condition & EXCEPTION_CONDITION)
679 gdk_cond |= GDK_INPUT_EXCEPTION;
681 if (closure->condition & gdk_cond)
682 closure->function (closure->data, g_io_channel_unix_get_fd (source), gdk_cond);
688 gdk_input_add_full (gint source,
689 GdkInputCondition condition,
690 GdkInputFunction function,
692 GdkDestroyNotify destroy)
695 GdkIOClosure *closure = g_new (GdkIOClosure, 1);
697 GIOCondition cond = 0;
699 closure->function = function;
700 closure->condition = condition;
701 closure->notify = destroy;
702 closure->data = data;
704 if (condition & GDK_INPUT_READ)
705 cond |= READ_CONDITION;
706 if (condition & GDK_INPUT_WRITE)
707 cond |= WRITE_CONDITION;
708 if (condition & GDK_INPUT_EXCEPTION)
709 cond |= EXCEPTION_CONDITION;
711 channel = g_io_channel_unix_new (source);
712 result = g_io_add_watch_full (channel, G_PRIORITY_DEFAULT, cond,
714 closure, gdk_io_destroy);
715 g_io_channel_unref (channel);
721 gdk_input_add (gint source,
722 GdkInputCondition condition,
723 GdkInputFunction function,
726 return gdk_input_add_full (source, condition, function, data, NULL);
730 gdk_input_remove (gint tag)
732 g_source_remove (tag);
736 gdk_event_unqueue (void)
738 GdkEvent *event = NULL;
741 tmp_list = gdk_event_queue_find_first ();
745 event = tmp_list->data;
746 gdk_event_queue_remove_link (tmp_list);
747 g_list_free_1 (tmp_list);
754 gdk_synthesize_click (GdkEvent *event,
759 g_return_if_fail (event != NULL);
762 temp_event.type = (nclicks == 2) ? GDK_2BUTTON_PRESS : GDK_3BUTTON_PRESS;
764 gdk_event_put (&temp_event);
768 gdk_event_button_generate (GdkEvent *event)
770 if ((event->button.time < (button_click_time[1] + TRIPLE_CLICK_TIME)) &&
771 (event->button.window == button_window[1]) &&
772 (event->button.button == button_number[1]))
774 gdk_synthesize_click (event, 3);
776 button_click_time[1] = 0;
777 button_click_time[0] = 0;
778 button_window[1] = NULL;
779 button_window[0] = 0;
780 button_number[1] = -1;
781 button_number[0] = -1;
783 else if ((event->button.time < (button_click_time[0] + DOUBLE_CLICK_TIME)) &&
784 (event->button.window == button_window[0]) &&
785 (event->button.button == button_number[0]))
787 gdk_synthesize_click (event, 2);
789 button_click_time[1] = button_click_time[0];
790 button_click_time[0] = event->button.time;
791 button_window[1] = button_window[0];
792 button_window[0] = event->button.window;
793 button_number[1] = button_number[0];
794 button_number[0] = event->button.button;
798 button_click_time[1] = 0;
799 button_click_time[0] = event->button.time;
800 button_window[1] = NULL;
801 button_window[0] = event->button.window;
802 button_number[1] = -1;
803 button_number[0] = event->button.button;