1
/* Metacity X event source for main loop */
4
* Copyright (C) 2001 Havoc Pennington (based on GDK code (C) Owen
5
* Taylor, Red Hat Inc.)
7
* This program is free software; you can redistribute it and/or
8
* modify it under the terms of the GNU General Public License as
9
* published by the Free Software Foundation; either version 2 of the
10
* License, or (at your option) any later version.
12
* This program is distributed in the hope that it will be useful, but
13
* WITHOUT ANY WARRANTY; without even the implied warranty of
14
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
* General Public License for more details.
17
* You should have received a copy of the GNU General Public License
18
* along with this program; if not, write to the Free Software
19
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22
#include "eventqueue.h"
25
static gboolean eq_prepare (GSource *source,
27
static gboolean eq_check (GSource *source);
28
static gboolean eq_dispatch (GSource *source,
31
static void eq_destroy (GSource *source);
33
static GSourceFuncs eq_funcs = {
40
struct _MetaEventQueue
51
meta_event_queue_new (Display *display, MetaEventQueueFunc func, gpointer data)
56
source = g_source_new (&eq_funcs, sizeof (MetaEventQueue));
57
eq = (MetaEventQueue*) source;
59
eq->connection_fd = ConnectionNumber (display);
60
eq->poll_fd.fd = eq->connection_fd;
61
eq->poll_fd.events = G_IO_IN;
63
eq->events = g_queue_new ();
65
eq->display = display;
67
g_source_set_priority (source, G_PRIORITY_DEFAULT);
68
g_source_add_poll (source, &eq->poll_fd);
69
g_source_set_can_recurse (source, TRUE);
71
g_source_set_callback (source, (GSourceFunc) func, data, NULL);
73
g_source_attach (source, NULL);
79
meta_event_queue_free (MetaEventQueue *eq)
83
source = (GSource*) eq;
85
g_source_destroy (source);
89
eq_events_pending (MetaEventQueue *eq)
91
return eq->events->length > 0 || XPending (eq->display);
95
eq_queue_events (MetaEventQueue *eq)
99
while (XPending (eq->display))
103
XNextEvent (eq->display, &xevent);
105
copy = g_new (XEvent, 1);
108
g_queue_push_tail (eq->events, copy);
113
eq_prepare (GSource *source, gint *timeout)
117
eq = (MetaEventQueue*) source;
121
return eq_events_pending (eq);
125
eq_check (GSource *source)
129
eq = (MetaEventQueue*) source;
131
if (eq->poll_fd.revents & G_IO_IN)
132
return eq_events_pending (eq);
138
eq_dispatch (GSource *source, GSourceFunc callback, gpointer user_data)
142
eq = (MetaEventQueue*) source;
144
eq_queue_events (eq);
146
if (eq->events->length > 0)
149
MetaEventQueueFunc func;
151
event = g_queue_pop_head (eq->events);
152
func = (MetaEventQueueFunc) callback;
154
(* func) (event, user_data);
163
eq_destroy (GSource *source)
167
eq = (MetaEventQueue*) source;
169
while (eq->events->length > 0)
173
event = g_queue_pop_head (eq->events);
178
g_queue_free (eq->events);
180
/* source itself is freed by glib */