summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog11
-rw-r--r--ChangeLog.pre-2-011
-rw-r--r--ChangeLog.pre-2-1011
-rw-r--r--ChangeLog.pre-2-1211
-rw-r--r--ChangeLog.pre-2-211
-rw-r--r--ChangeLog.pre-2-411
-rw-r--r--ChangeLog.pre-2-611
-rw-r--r--ChangeLog.pre-2-811
-rw-r--r--Makefile.am3
-rw-r--r--acconfig.h2
-rw-r--r--configure.in10
-rw-r--r--giochannel.c164
-rw-r--r--giounix.c293
-rw-r--r--glib.h205
-rw-r--r--glib/Makefile.am3
-rw-r--r--glib/giochannel.c164
-rw-r--r--glib/giounix.c293
-rw-r--r--glib/glib.h205
-rw-r--r--glib/glist.c59
-rw-r--r--glib/gmain.c731
-rw-r--r--glib/gutils.c3
-rw-r--r--glist.c59
-rw-r--r--gmain.c731
-rw-r--r--gutils.c3
24 files changed, 3013 insertions, 3 deletions
diff --git a/ChangeLog b/ChangeLog
index b779d9ed3..de086d999 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/ChangeLog.pre-2-0 b/ChangeLog.pre-2-0
index b779d9ed3..de086d999 100644
--- a/ChangeLog.pre-2-0
+++ b/ChangeLog.pre-2-0
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/ChangeLog.pre-2-10 b/ChangeLog.pre-2-10
index b779d9ed3..de086d999 100644
--- a/ChangeLog.pre-2-10
+++ b/ChangeLog.pre-2-10
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/ChangeLog.pre-2-12 b/ChangeLog.pre-2-12
index b779d9ed3..de086d999 100644
--- a/ChangeLog.pre-2-12
+++ b/ChangeLog.pre-2-12
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/ChangeLog.pre-2-2 b/ChangeLog.pre-2-2
index b779d9ed3..de086d999 100644
--- a/ChangeLog.pre-2-2
+++ b/ChangeLog.pre-2-2
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/ChangeLog.pre-2-4 b/ChangeLog.pre-2-4
index b779d9ed3..de086d999 100644
--- a/ChangeLog.pre-2-4
+++ b/ChangeLog.pre-2-4
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/ChangeLog.pre-2-6 b/ChangeLog.pre-2-6
index b779d9ed3..de086d999 100644
--- a/ChangeLog.pre-2-6
+++ b/ChangeLog.pre-2-6
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/ChangeLog.pre-2-8 b/ChangeLog.pre-2-8
index b779d9ed3..de086d999 100644
--- a/ChangeLog.pre-2-8
+++ b/ChangeLog.pre-2-8
@@ -1,3 +1,14 @@
+Sat Nov 28 12:53:47 1998 Owen Taylor <otaylor@redhat.com>
+
+ * Makefile.am configure.in acconfig.h giochannel.c
+ glib.h glist.c gmain.c gutils.c:
+
+ - Revised GIOChannel to provide a generic virtual-function
+ based interface.
+ - Added unix fd-based GIOChannel's
+ - Added generic main-loop abstraction
+ - Added timeouts and idle functions using main-loop abstraction.
+
1998-12-02 Sebastian Wilhelmi <wilhelmi@ira.uka.de>
* glib.h:
diff --git a/Makefile.am b/Makefile.am
index 99ee6ee7b..85c46f4a4 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -36,6 +36,9 @@ libglib_la_SOURCES = \
ghash.c \
ghook.c \
glist.c \
+ gmain.c \
+ giochannel.c \
+ giounix.c \
gmem.c \
gmessages.c \
gnode.c \
diff --git a/acconfig.h b/acconfig.h
index e2daff8c4..93ecd556d 100644
--- a/acconfig.h
+++ b/acconfig.h
@@ -43,8 +43,10 @@
#undef HAVE_FLOAT_H
#undef HAVE_LIMITS_H
#undef HAVE_LONG_DOUBLE
+#undef HAVE_POLL
#undef HAVE_PWD_H
#undef HAVE_SYS_PARAM_H
+#undef HAVE_SYS_POLL_H
#undef HAVE_SYS_SELECT_H
#undef HAVE_SYS_TIME_H
#undef HAVE_SYS_TIMES_H
diff --git a/configure.in b/configure.in
index 8e104e7e0..00817003e 100644
--- a/configure.in
+++ b/configure.in
@@ -226,6 +226,7 @@ AC_CHECK_HEADERS(float.h, AC_DEFINE(HAVE_FLOAT_H))
AC_CHECK_HEADERS(limits.h, AC_DEFINE(HAVE_LIMITS_H))
AC_CHECK_HEADERS(pwd.h, AC_DEFINE(HAVE_PWD_H))
AC_CHECK_HEADERS(sys/param.h, AC_DEFINE(HAVE_SYS_PARAM_H))
+AC_CHECK_HEADERS(sys/poll.h, AC_DEFINE(HAVE_SYS_POLL_H))
AC_CHECK_HEADERS(sys/select.h, AC_DEFINE(HAVE_SYS_SELECT_H))
AC_CHECK_HEADERS(sys/time.h, AC_DEFINE(HAVE_SYS_TIME_H))
AC_CHECK_HEADERS(sys/times.h, AC_DEFINE(HAVE_SYS_TIMES_H))
@@ -233,7 +234,7 @@ AC_CHECK_HEADERS(unistd.h, AC_DEFINE(HAVE_UNISTD_H))
AC_CHECK_HEADERS(values.h, AC_DEFINE(HAVE_VALUES_H))
# Check for some functions
-AC_CHECK_FUNCS(lstat strerror strsignal memmove vsnprintf strcasecmp strncasecmp)
+AC_CHECK_FUNCS(lstat strerror strsignal memmove vsnprintf strcasecmp strncasecmp poll)
# Check for sys_errlist
AC_MSG_CHECKING(for sys_errlist)
@@ -549,6 +550,9 @@ outfile_EOF
if test x$glib_values_h = xyes; then
echo '#include <values.h>' >> $outfile
fi
+ if test x$glib_sys_poll_h = xyes; then
+ echo '#include <sys/poll.h>' >> $outfile
+ fi
cat >> $outfile <<outfile_EOF
@@ -697,6 +701,10 @@ x$ac_cv_header_values_h)
;;
esac
+if test x$ac_cv_header_sys_poll_h = xyes ; then
+ glib_sys_poll_h=yes
+fi
+
case 2 in
$ac_cv_sizeof_short) gint16=short;;
$ac_cv_sizeof_int) gint16=int;;
diff --git a/giochannel.c b/giochannel.c
new file mode 100644
index 000000000..fa4262602
--- /dev/null
+++ b/giochannel.c
@@ -0,0 +1,164 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * giochannel.c: IO Channel abstraction
+ * Copyright 1998 Owen Taylor
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "glib.h"
+#include <unistd.h>
+
+typedef struct _GIOChannelPrivate GIOChannelPrivate;
+
+struct _GIOChannelPrivate {
+ GIOChannel channel;
+ GIOFuncs *funcs;
+ guint ref_count;
+ gboolean closed;
+};
+
+GIOChannel *
+g_io_channel_new (GIOFuncs *funcs,
+ gpointer channel_data)
+{
+ GIOChannelPrivate *result;
+ GIOChannel *channel;
+
+ g_return_val_if_fail (funcs != NULL, NULL);
+
+ result = g_new (GIOChannelPrivate, 1);
+ channel = (GIOChannel *)result;
+
+ result->funcs = funcs;
+ result->ref_count = 1;
+ result->closed = FALSE;
+
+ channel->channel_data = channel_data;
+ return channel;
+}
+
+
+void
+g_io_channel_ref (GIOChannel *channel)
+{
+ GIOChannelPrivate *private;
+ g_return_if_fail (channel != NULL);
+
+ private = (GIOChannelPrivate *)channel;
+
+ private->ref_count++;
+}
+
+void
+g_io_channel_unref (GIOChannel *channel)
+{
+ GIOChannelPrivate *private;
+ g_return_if_fail (channel != NULL);
+
+ private = (GIOChannelPrivate *)channel;
+
+ private->ref_count--;
+ if (private->ref_count == 0)
+ {
+ /* We don't want to close the channel here, because
+ * the channel may just be wrapping a file or socket
+ * that the app is independently manipulating.
+ */
+ private->funcs->io_free (channel);
+ g_free (private);
+ }
+}
+
+GIOError
+g_io_channel_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
+ g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
+
+ return private->funcs->io_read (channel, buf, count, bytes_read);
+}
+
+GIOError
+g_io_channel_write (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
+ g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
+
+ return private->funcs->io_write (channel, buf, count, bytes_written);
+}
+
+GIOError
+g_io_channel_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
+ g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
+
+ return private->funcs->io_seek (channel, offset, type);
+}
+
+void
+g_io_channel_close (GIOChannel *channel)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_if_fail (channel != NULL);
+ g_return_if_fail (!private->closed);
+
+ private->closed = TRUE;
+ private->funcs->io_close (channel);
+}
+
+guint
+g_io_add_watch_full (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, 0);
+ g_return_val_if_fail (!private->closed, 0);
+
+ return private->funcs->io_add_watch (channel, priority, condition,
+ func, user_data, notify);
+}
+
+guint
+g_io_add_watch (GIOChannel *channel,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data)
+{
+ return g_io_add_watch_full (channel, 0, condition, func, user_data, NULL);
+}
diff --git a/giounix.c b/giounix.c
new file mode 100644
index 000000000..5e605a478
--- /dev/null
+++ b/giounix.c
@@ -0,0 +1,293 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * giounix.c: IO Channels using unix file descriptors
+ * Copyright 1998 Owen Taylor
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "glib.h"
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+
+/*
+ * Unix IO Channels
+ */
+
+typedef struct _GIOUnixChannel GIOUnixChannel;
+typedef struct _GIOUnixWatch GIOUnixWatch;
+
+struct _GIOUnixChannel {
+ gint fd;
+};
+
+struct _GIOUnixWatch {
+ GPollFD pollfd;
+ GIOChannel *channel;
+ GIOCondition condition;
+ GIOFunc callback;
+};
+
+
+static GIOError g_io_unix_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+
+static GIOError g_io_unix_write(GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+static GIOError g_io_unix_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type);
+static void g_io_unix_close (GIOChannel *channel);
+static void g_io_unix_free (GIOChannel *channel);
+static guint g_io_unix_add_watch (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify);
+static gboolean g_io_unix_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+static gboolean g_io_unix_check (gpointer source_data,
+ GTimeVal *current_time);
+static gboolean g_io_unix_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+static void g_io_unix_destroy (gpointer source_data);
+
+GSourceFuncs unix_watch_funcs = {
+ g_io_unix_prepare,
+ g_io_unix_check,
+ g_io_unix_dispatch,
+ g_io_unix_destroy
+};
+
+GIOFuncs unix_channel_funcs = {
+ g_io_unix_read,
+ g_io_unix_write,
+ g_io_unix_seek,
+ g_io_unix_close,
+ g_io_unix_add_watch,
+ g_io_unix_free,
+};
+
+static gboolean
+g_io_unix_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout)
+{
+ *timeout = -1;
+ return FALSE;
+}
+
+static gboolean
+g_io_unix_check (gpointer source_data,
+ GTimeVal *current_time)
+{
+ GIOUnixWatch *data = source_data;
+
+ return (data->pollfd.revents & data->condition);
+}
+
+static gboolean
+g_io_unix_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data)
+
+{
+ GIOUnixWatch *data = source_data;
+
+ return (*data->callback)(data->channel,
+ data->pollfd.revents & data->condition,
+ user_data);
+}
+
+static void
+g_io_unix_destroy (gpointer source_data)
+{
+ GIOUnixWatch *data = source_data;
+
+ g_main_poll_remove (&data->pollfd);
+ g_io_channel_unref (data->channel);
+ g_free (data);
+}
+
+static GIOError
+g_io_unix_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ gint result;
+
+ result = read (unix_channel->fd, buf, count);
+
+ if (result < 0)
+ {
+ *bytes_read = 0;
+ switch (errno)
+ {
+ case EINVAL:
+ return G_IO_ERROR_INVAL;
+ case EAGAIN:
+ return G_IO_ERROR_AGAIN;
+ default:
+ return G_IO_ERROR_UNKNOWN;
+ }
+ }
+ else
+ {
+ *bytes_read = result;
+ return G_IO_ERROR_NONE;
+ }
+}
+
+static GIOError
+g_io_unix_write(GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ gint result;
+
+ result = write (unix_channel->fd, buf, count);
+
+ if (result < 0)
+ {
+ *bytes_written = 0;
+ switch (errno)
+ {
+ case EINVAL:
+ return G_IO_ERROR_INVAL;
+ case EAGAIN:
+ return G_IO_ERROR_AGAIN;
+ default:
+ return G_IO_ERROR_UNKNOWN;
+ }
+ }
+ else
+ {
+ *bytes_written = result;
+ return G_IO_ERROR_NONE;
+ }
+}
+
+static GIOError
+g_io_unix_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ int whence;
+ off_t result;
+
+ switch (type)
+ {
+ case G_SEEK_SET:
+ whence = SEEK_SET;
+ break;
+ case G_SEEK_CUR:
+ whence = SEEK_CUR;
+ break;
+ case G_SEEK_END:
+ whence = SEEK_END;
+ break;
+ default:
+ g_warning ("g_io_unix_seek: unknown seek type");
+ return G_IO_ERROR_UNKNOWN;
+ }
+
+ result = lseek (unix_channel->fd, offset, whence);
+
+ if (result < 0)
+ {
+ switch (errno)
+ {
+ case EINVAL:
+ return G_IO_ERROR_INVAL;
+ default:
+ return G_IO_ERROR_UNKNOWN;
+ }
+ }
+ else
+ return G_IO_ERROR_NONE;
+}
+
+
+static void
+g_io_unix_close (GIOChannel *channel)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+
+ close (unix_channel->fd);
+}
+
+static void
+g_io_unix_free (GIOChannel *channel)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+
+ g_free (unix_channel);
+}
+
+static guint
+g_io_unix_add_watch (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify)
+{
+ GIOUnixWatch *watch = g_new (GIOUnixWatch, 1);
+ GIOUnixChannel *unix_channel = channel->channel_data;
+
+ watch->channel = channel;
+ g_io_channel_ref (channel);
+
+ watch->callback = func;
+ watch->condition = condition;
+
+ watch->pollfd.fd = unix_channel->fd;
+ watch->pollfd.events = condition;
+
+ g_main_poll_add (priority, &watch->pollfd);
+
+ return g_source_add (priority, TRUE, &unix_watch_funcs, watch, user_data, notify);
+}
+
+GIOChannel *
+g_io_channel_unix_new (gint fd)
+{
+ GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
+
+ unix_channel->fd = fd;
+ return g_io_channel_new (&unix_channel_funcs, unix_channel);
+}
+
+gint
+g_io_channel_unix_get_fd (GIOChannel *channel)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ return unix_channel->fd;
+}
diff --git a/glib.h b/glib.h
index 3702c26f5..fa9c8a725 100644
--- a/glib.h
+++ b/glib.h
@@ -155,7 +155,7 @@ extern "C" {
* we define G_CAN_INLINE, if the compiler seems to be actually
* *capable* to do function inlining, in which case inline function bodys
* do make sense. we also define G_INLINE_FUNC to properly export the
- * function prototypes if no inlinig can be performed.
+ * function prototypes if no inlining can be performed.
* we special case most of the stuff, so inline functions can have a normal
* implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
*/
@@ -2295,6 +2295,208 @@ gpointer g_tuples_index (GTuples *tuples,
guint g_spaced_primes_closest (guint num);
+/* IO Channels
+ */
+
+typedef struct _GIOFuncs GIOFuncs;
+
+typedef enum {
+ G_IO_ERROR_NONE,
+ G_IO_ERROR_AGAIN,
+ G_IO_ERROR_INVAL,
+ G_IO_ERROR_UNKNOWN
+} GIOError;
+
+typedef enum {
+ G_SEEK_CUR,
+ G_SEEK_SET,
+ G_SEEK_END
+} GSeekType;
+
+typedef enum {
+ G_IO_IN
+#ifdef POLLIN
+ = POLLIN
+#endif
+ ,G_IO_OUT
+#ifdef POLLOUT
+ = POLLOUT
+#endif
+ ,G_IO_PRI
+#ifdef POLLPRI
+ = POLLPRI
+#endif
+
+ ,G_IO_ERR
+#ifdef POLLERR
+ = POLLERR
+#endif
+ ,G_IO_HUP
+#ifdef POLLHUP
+ = POLLHUP
+#endif
+ ,G_IO_NVAL
+#ifdef POLLNVAL
+ = POLLNVAL
+#endif
+} GIOCondition;
+
+struct _GIOChannel {
+ gpointer channel_data;
+};
+
+typedef gboolean (*GIOFunc) (GIOChannel *source,
+ GIOCondition condition,
+ gpointer data);
+
+struct _GIOFuncs {
+ GIOError (*io_read) (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read);
+ GIOError (*io_write) (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+ GIOError (*io_seek) (GIOChannel *channel,
+ gint offset,
+ GSeekType type);
+ void (*io_close) (GIOChannel *channel);
+ guint (*io_add_watch) (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify);
+ void (*io_free) (GIOChannel *channel);
+};
+
+GIOChannel *g_io_channel_new (GIOFuncs *funcs,
+ gpointer channel_data);
+void g_io_channel_ref (GIOChannel *channel);
+void g_io_channel_unref (GIOChannel *channel);
+GIOError g_io_channel_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read);
+GIOError g_io_channel_write (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+GIOError g_io_channel_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type);
+void g_io_channel_close (GIOChannel *channel);
+guint g_io_add_watch_full (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify);
+guint g_io_add_watch (GIOChannel *channel,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data);
+
+/* Main loop */
+
+typedef struct _GTimeVal GTimeVal;
+typedef struct _GSourceFuncs GSourceFuncs;
+
+typedef struct _GMainLoop GMainLoop; /* Opaque */
+
+struct _GTimeVal {
+ glong tv_sec;
+ glong tv_usec;
+};
+
+struct _GSourceFuncs {
+ gboolean (*prepare) (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+ gboolean (*check) (gpointer source_data,
+ GTimeVal *current_time);
+ gboolean (*dispatch) (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+ GDestroyNotify destroy;
+};
+
+typedef gboolean (*GSourceFunc) (gpointer data);
+
+/* Hooks for adding to the main loop */
+
+guint g_source_add (gint priority,
+ gboolean can_recurse,
+ GSourceFuncs *funcs,
+ gpointer source_data,
+ gpointer user_data,
+ GDestroyNotify notify);
+void g_source_remove (guint tag);
+void g_source_remove_by_user_data (gpointer user_data);
+void g_source_remove_by_source_data (gpointer source_data);
+
+
+void g_get_current_time (GTimeVal *result);
+
+/* Running the main loop */
+
+GMainLoop *g_main_new (void);
+void g_main_run (GMainLoop *loop);
+void g_main_quit (GMainLoop *loop);
+void g_main_destroy (GMainLoop *loop);
+
+/* Run a single iteration of the mainloop. If block is FALSE,
+ * will never block
+ */
+gboolean g_main_iteration (gboolean block);
+
+/* See if any events are pending
+ */
+gboolean g_main_pending ();
+
+/* Idles and timeouts */
+
+guint g_timeout_add_full (gint priority,
+ guint interval,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify notify);
+guint g_timeout_add (guint interval,
+ GSourceFunc function,
+ gpointer data);
+
+guint g_idle_add (GSourceFunc function,
+ gpointer data);
+guint g_idle_add_full (gint priority,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify destroy);
+
+/* Unix-specific IO and main loop calls */
+
+typedef struct _GPollFD GPollFD;
+
+struct _GPollFD {
+ gint fd;
+ gushort events;
+ gushort revents;
+};
+
+typedef gint (*GPollFunc) (GPollFD *ufds, guint nfsd, gint timeout);
+
+void g_main_poll_add (gint priority,
+ GPollFD *fd);
+void g_main_poll_remove (GPollFD *fd);
+
+void g_main_set_poll_func (GPollFunc func);
+
+
+GIOChannel *g_io_channel_unix_new (int fd);
+gint g_io_channel_unix_get_fd (GIOChannel *channel);
+
+#if 0 /* old IO Channels */
+
/* IO Channels.
* These are used for plug-in communication in the GIMP, for instance.
* On Unix, it's simply an encapsulated file descriptor (a pipe).
@@ -2326,6 +2528,7 @@ void g_iochannel_wakeup_peer (GIOChannel *channel);
# define g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
#endif
+#endif /* old IO Channels */
/* Windows emulation stubs for common unix functions
*/
diff --git a/glib/Makefile.am b/glib/Makefile.am
index 99ee6ee7b..85c46f4a4 100644
--- a/glib/Makefile.am
+++ b/glib/Makefile.am
@@ -36,6 +36,9 @@ libglib_la_SOURCES = \
ghash.c \
ghook.c \
glist.c \
+ gmain.c \
+ giochannel.c \
+ giounix.c \
gmem.c \
gmessages.c \
gnode.c \
diff --git a/glib/giochannel.c b/glib/giochannel.c
new file mode 100644
index 000000000..fa4262602
--- /dev/null
+++ b/glib/giochannel.c
@@ -0,0 +1,164 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * giochannel.c: IO Channel abstraction
+ * Copyright 1998 Owen Taylor
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "glib.h"
+#include <unistd.h>
+
+typedef struct _GIOChannelPrivate GIOChannelPrivate;
+
+struct _GIOChannelPrivate {
+ GIOChannel channel;
+ GIOFuncs *funcs;
+ guint ref_count;
+ gboolean closed;
+};
+
+GIOChannel *
+g_io_channel_new (GIOFuncs *funcs,
+ gpointer channel_data)
+{
+ GIOChannelPrivate *result;
+ GIOChannel *channel;
+
+ g_return_val_if_fail (funcs != NULL, NULL);
+
+ result = g_new (GIOChannelPrivate, 1);
+ channel = (GIOChannel *)result;
+
+ result->funcs = funcs;
+ result->ref_count = 1;
+ result->closed = FALSE;
+
+ channel->channel_data = channel_data;
+ return channel;
+}
+
+
+void
+g_io_channel_ref (GIOChannel *channel)
+{
+ GIOChannelPrivate *private;
+ g_return_if_fail (channel != NULL);
+
+ private = (GIOChannelPrivate *)channel;
+
+ private->ref_count++;
+}
+
+void
+g_io_channel_unref (GIOChannel *channel)
+{
+ GIOChannelPrivate *private;
+ g_return_if_fail (channel != NULL);
+
+ private = (GIOChannelPrivate *)channel;
+
+ private->ref_count--;
+ if (private->ref_count == 0)
+ {
+ /* We don't want to close the channel here, because
+ * the channel may just be wrapping a file or socket
+ * that the app is independently manipulating.
+ */
+ private->funcs->io_free (channel);
+ g_free (private);
+ }
+}
+
+GIOError
+g_io_channel_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
+ g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
+
+ return private->funcs->io_read (channel, buf, count, bytes_read);
+}
+
+GIOError
+g_io_channel_write (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
+ g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
+
+ return private->funcs->io_write (channel, buf, count, bytes_written);
+}
+
+GIOError
+g_io_channel_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, G_IO_ERROR_UNKNOWN);
+ g_return_val_if_fail (!private->closed, G_IO_ERROR_UNKNOWN);
+
+ return private->funcs->io_seek (channel, offset, type);
+}
+
+void
+g_io_channel_close (GIOChannel *channel)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_if_fail (channel != NULL);
+ g_return_if_fail (!private->closed);
+
+ private->closed = TRUE;
+ private->funcs->io_close (channel);
+}
+
+guint
+g_io_add_watch_full (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify)
+{
+ GIOChannelPrivate *private = (GIOChannelPrivate *)channel;
+
+ g_return_val_if_fail (channel != NULL, 0);
+ g_return_val_if_fail (!private->closed, 0);
+
+ return private->funcs->io_add_watch (channel, priority, condition,
+ func, user_data, notify);
+}
+
+guint
+g_io_add_watch (GIOChannel *channel,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data)
+{
+ return g_io_add_watch_full (channel, 0, condition, func, user_data, NULL);
+}
diff --git a/glib/giounix.c b/glib/giounix.c
new file mode 100644
index 000000000..5e605a478
--- /dev/null
+++ b/glib/giounix.c
@@ -0,0 +1,293 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * giounix.c: IO Channels using unix file descriptors
+ * Copyright 1998 Owen Taylor
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "glib.h"
+#include <sys/types.h>
+#include <unistd.h>
+#include <errno.h>
+
+/*
+ * Unix IO Channels
+ */
+
+typedef struct _GIOUnixChannel GIOUnixChannel;
+typedef struct _GIOUnixWatch GIOUnixWatch;
+
+struct _GIOUnixChannel {
+ gint fd;
+};
+
+struct _GIOUnixWatch {
+ GPollFD pollfd;
+ GIOChannel *channel;
+ GIOCondition condition;
+ GIOFunc callback;
+};
+
+
+static GIOError g_io_unix_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+
+static GIOError g_io_unix_write(GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+static GIOError g_io_unix_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type);
+static void g_io_unix_close (GIOChannel *channel);
+static void g_io_unix_free (GIOChannel *channel);
+static guint g_io_unix_add_watch (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify);
+static gboolean g_io_unix_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+static gboolean g_io_unix_check (gpointer source_data,
+ GTimeVal *current_time);
+static gboolean g_io_unix_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+static void g_io_unix_destroy (gpointer source_data);
+
+GSourceFuncs unix_watch_funcs = {
+ g_io_unix_prepare,
+ g_io_unix_check,
+ g_io_unix_dispatch,
+ g_io_unix_destroy
+};
+
+GIOFuncs unix_channel_funcs = {
+ g_io_unix_read,
+ g_io_unix_write,
+ g_io_unix_seek,
+ g_io_unix_close,
+ g_io_unix_add_watch,
+ g_io_unix_free,
+};
+
+static gboolean
+g_io_unix_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout)
+{
+ *timeout = -1;
+ return FALSE;
+}
+
+static gboolean
+g_io_unix_check (gpointer source_data,
+ GTimeVal *current_time)
+{
+ GIOUnixWatch *data = source_data;
+
+ return (data->pollfd.revents & data->condition);
+}
+
+static gboolean
+g_io_unix_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data)
+
+{
+ GIOUnixWatch *data = source_data;
+
+ return (*data->callback)(data->channel,
+ data->pollfd.revents & data->condition,
+ user_data);
+}
+
+static void
+g_io_unix_destroy (gpointer source_data)
+{
+ GIOUnixWatch *data = source_data;
+
+ g_main_poll_remove (&data->pollfd);
+ g_io_channel_unref (data->channel);
+ g_free (data);
+}
+
+static GIOError
+g_io_unix_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ gint result;
+
+ result = read (unix_channel->fd, buf, count);
+
+ if (result < 0)
+ {
+ *bytes_read = 0;
+ switch (errno)
+ {
+ case EINVAL:
+ return G_IO_ERROR_INVAL;
+ case EAGAIN:
+ return G_IO_ERROR_AGAIN;
+ default:
+ return G_IO_ERROR_UNKNOWN;
+ }
+ }
+ else
+ {
+ *bytes_read = result;
+ return G_IO_ERROR_NONE;
+ }
+}
+
+static GIOError
+g_io_unix_write(GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ gint result;
+
+ result = write (unix_channel->fd, buf, count);
+
+ if (result < 0)
+ {
+ *bytes_written = 0;
+ switch (errno)
+ {
+ case EINVAL:
+ return G_IO_ERROR_INVAL;
+ case EAGAIN:
+ return G_IO_ERROR_AGAIN;
+ default:
+ return G_IO_ERROR_UNKNOWN;
+ }
+ }
+ else
+ {
+ *bytes_written = result;
+ return G_IO_ERROR_NONE;
+ }
+}
+
+static GIOError
+g_io_unix_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ int whence;
+ off_t result;
+
+ switch (type)
+ {
+ case G_SEEK_SET:
+ whence = SEEK_SET;
+ break;
+ case G_SEEK_CUR:
+ whence = SEEK_CUR;
+ break;
+ case G_SEEK_END:
+ whence = SEEK_END;
+ break;
+ default:
+ g_warning ("g_io_unix_seek: unknown seek type");
+ return G_IO_ERROR_UNKNOWN;
+ }
+
+ result = lseek (unix_channel->fd, offset, whence);
+
+ if (result < 0)
+ {
+ switch (errno)
+ {
+ case EINVAL:
+ return G_IO_ERROR_INVAL;
+ default:
+ return G_IO_ERROR_UNKNOWN;
+ }
+ }
+ else
+ return G_IO_ERROR_NONE;
+}
+
+
+static void
+g_io_unix_close (GIOChannel *channel)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+
+ close (unix_channel->fd);
+}
+
+static void
+g_io_unix_free (GIOChannel *channel)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+
+ g_free (unix_channel);
+}
+
+static guint
+g_io_unix_add_watch (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify)
+{
+ GIOUnixWatch *watch = g_new (GIOUnixWatch, 1);
+ GIOUnixChannel *unix_channel = channel->channel_data;
+
+ watch->channel = channel;
+ g_io_channel_ref (channel);
+
+ watch->callback = func;
+ watch->condition = condition;
+
+ watch->pollfd.fd = unix_channel->fd;
+ watch->pollfd.events = condition;
+
+ g_main_poll_add (priority, &watch->pollfd);
+
+ return g_source_add (priority, TRUE, &unix_watch_funcs, watch, user_data, notify);
+}
+
+GIOChannel *
+g_io_channel_unix_new (gint fd)
+{
+ GIOUnixChannel *unix_channel = g_new (GIOUnixChannel, 1);
+
+ unix_channel->fd = fd;
+ return g_io_channel_new (&unix_channel_funcs, unix_channel);
+}
+
+gint
+g_io_channel_unix_get_fd (GIOChannel *channel)
+{
+ GIOUnixChannel *unix_channel = channel->channel_data;
+ return unix_channel->fd;
+}
diff --git a/glib/glib.h b/glib/glib.h
index 3702c26f5..fa9c8a725 100644
--- a/glib/glib.h
+++ b/glib/glib.h
@@ -155,7 +155,7 @@ extern "C" {
* we define G_CAN_INLINE, if the compiler seems to be actually
* *capable* to do function inlining, in which case inline function bodys
* do make sense. we also define G_INLINE_FUNC to properly export the
- * function prototypes if no inlinig can be performed.
+ * function prototypes if no inlining can be performed.
* we special case most of the stuff, so inline functions can have a normal
* implementation by defining G_INLINE_FUNC to extern and G_CAN_INLINE to 1.
*/
@@ -2295,6 +2295,208 @@ gpointer g_tuples_index (GTuples *tuples,
guint g_spaced_primes_closest (guint num);
+/* IO Channels
+ */
+
+typedef struct _GIOFuncs GIOFuncs;
+
+typedef enum {
+ G_IO_ERROR_NONE,
+ G_IO_ERROR_AGAIN,
+ G_IO_ERROR_INVAL,
+ G_IO_ERROR_UNKNOWN
+} GIOError;
+
+typedef enum {
+ G_SEEK_CUR,
+ G_SEEK_SET,
+ G_SEEK_END
+} GSeekType;
+
+typedef enum {
+ G_IO_IN
+#ifdef POLLIN
+ = POLLIN
+#endif
+ ,G_IO_OUT
+#ifdef POLLOUT
+ = POLLOUT
+#endif
+ ,G_IO_PRI
+#ifdef POLLPRI
+ = POLLPRI
+#endif
+
+ ,G_IO_ERR
+#ifdef POLLERR
+ = POLLERR
+#endif
+ ,G_IO_HUP
+#ifdef POLLHUP
+ = POLLHUP
+#endif
+ ,G_IO_NVAL
+#ifdef POLLNVAL
+ = POLLNVAL
+#endif
+} GIOCondition;
+
+struct _GIOChannel {
+ gpointer channel_data;
+};
+
+typedef gboolean (*GIOFunc) (GIOChannel *source,
+ GIOCondition condition,
+ gpointer data);
+
+struct _GIOFuncs {
+ GIOError (*io_read) (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read);
+ GIOError (*io_write) (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+ GIOError (*io_seek) (GIOChannel *channel,
+ gint offset,
+ GSeekType type);
+ void (*io_close) (GIOChannel *channel);
+ guint (*io_add_watch) (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify);
+ void (*io_free) (GIOChannel *channel);
+};
+
+GIOChannel *g_io_channel_new (GIOFuncs *funcs,
+ gpointer channel_data);
+void g_io_channel_ref (GIOChannel *channel);
+void g_io_channel_unref (GIOChannel *channel);
+GIOError g_io_channel_read (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_read);
+GIOError g_io_channel_write (GIOChannel *channel,
+ gchar *buf,
+ guint count,
+ guint *bytes_written);
+GIOError g_io_channel_seek (GIOChannel *channel,
+ gint offset,
+ GSeekType type);
+void g_io_channel_close (GIOChannel *channel);
+guint g_io_add_watch_full (GIOChannel *channel,
+ gint priority,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data,
+ GDestroyNotify notify);
+guint g_io_add_watch (GIOChannel *channel,
+ GIOCondition condition,
+ GIOFunc func,
+ gpointer user_data);
+
+/* Main loop */
+
+typedef struct _GTimeVal GTimeVal;
+typedef struct _GSourceFuncs GSourceFuncs;
+
+typedef struct _GMainLoop GMainLoop; /* Opaque */
+
+struct _GTimeVal {
+ glong tv_sec;
+ glong tv_usec;
+};
+
+struct _GSourceFuncs {
+ gboolean (*prepare) (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+ gboolean (*check) (gpointer source_data,
+ GTimeVal *current_time);
+ gboolean (*dispatch) (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+ GDestroyNotify destroy;
+};
+
+typedef gboolean (*GSourceFunc) (gpointer data);
+
+/* Hooks for adding to the main loop */
+
+guint g_source_add (gint priority,
+ gboolean can_recurse,
+ GSourceFuncs *funcs,
+ gpointer source_data,
+ gpointer user_data,
+ GDestroyNotify notify);
+void g_source_remove (guint tag);
+void g_source_remove_by_user_data (gpointer user_data);
+void g_source_remove_by_source_data (gpointer source_data);
+
+
+void g_get_current_time (GTimeVal *result);
+
+/* Running the main loop */
+
+GMainLoop *g_main_new (void);
+void g_main_run (GMainLoop *loop);
+void g_main_quit (GMainLoop *loop);
+void g_main_destroy (GMainLoop *loop);
+
+/* Run a single iteration of the mainloop. If block is FALSE,
+ * will never block
+ */
+gboolean g_main_iteration (gboolean block);
+
+/* See if any events are pending
+ */
+gboolean g_main_pending ();
+
+/* Idles and timeouts */
+
+guint g_timeout_add_full (gint priority,
+ guint interval,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify notify);
+guint g_timeout_add (guint interval,
+ GSourceFunc function,
+ gpointer data);
+
+guint g_idle_add (GSourceFunc function,
+ gpointer data);
+guint g_idle_add_full (gint priority,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify destroy);
+
+/* Unix-specific IO and main loop calls */
+
+typedef struct _GPollFD GPollFD;
+
+struct _GPollFD {
+ gint fd;
+ gushort events;
+ gushort revents;
+};
+
+typedef gint (*GPollFunc) (GPollFD *ufds, guint nfsd, gint timeout);
+
+void g_main_poll_add (gint priority,
+ GPollFD *fd);
+void g_main_poll_remove (GPollFD *fd);
+
+void g_main_set_poll_func (GPollFunc func);
+
+
+GIOChannel *g_io_channel_unix_new (int fd);
+gint g_io_channel_unix_get_fd (GIOChannel *channel);
+
+#if 0 /* old IO Channels */
+
/* IO Channels.
* These are used for plug-in communication in the GIMP, for instance.
* On Unix, it's simply an encapsulated file descriptor (a pipe).
@@ -2326,6 +2528,7 @@ void g_iochannel_wakeup_peer (GIOChannel *channel);
# define g_iochannel_wakeup_peer(channel) G_STMT_START { } G_STMT_END
#endif
+#endif /* old IO Channels */
/* Windows emulation stubs for common unix functions
*/
diff --git a/glib/glist.c b/glib/glist.c
index ab69826a2..41a09dd18 100644
--- a/glib/glist.c
+++ b/glib/glist.c
@@ -574,3 +574,62 @@ g_list_sort (GList *list,
g_list_sort (l2, compare_func),
compare_func);
}
+
+GList*
+g_list_sort2 (GList *list,
+ GCompareFunc compare_func)
+{
+ GSList *runs = NULL;
+ GList *tmp;
+
+ /* Degenerate case. */
+ if (!list) return NULL;
+
+ /* Assume: list = [12,2,4,11,2,4,6,1,1,12]. */
+ for (tmp = list; tmp; )
+ {
+ GList *tmp2;
+ for (tmp2 = tmp;
+ tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
+ tmp2 = tmp2->next)
+ /* Nothing */;
+ runs = g_slist_append (runs, tmp);
+ tmp = tmp2->next;
+ tmp2->next = NULL;
+ }
+ /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]]. */
+
+ while (runs->next)
+ {
+ /* We have more than one run. Merge pairwise. */
+ GSList *dst, *src, *dstprev = NULL;
+ dst = src = runs;
+ while (src && src->next)
+ {
+ dst->data = g_list_sort_merge (src->data,
+ src->next->data,
+ compare_func);
+ dstprev = dst;
+ dst = dst->next;
+ src = src->next->next;
+ }
+
+ /* If number of runs was odd, just keep the last. */
+ if (src)
+ {
+ dst->data = src->data;
+ dstprev = dst;
+ dst = dst->next;
+ }
+
+ dstprev->next = NULL;
+ g_slist_free (dst);
+ }
+
+ /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]]. */
+ /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]]. */
+
+ list = runs->data;
+ g_slist_free (runs);
+ return list;
+}
diff --git a/glib/gmain.c b/glib/gmain.c
new file mode 100644
index 000000000..79c1191f1
--- /dev/null
+++ b/glib/gmain.c
@@ -0,0 +1,731 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * gmain.c: Main loop abstraction, timeouts, and idle functions
+ * Copyright 1998 Owen Taylor
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "glib.h"
+#include <sys/time.h>
+#include <unistd.h>
+#include "config.h"
+
+/* Types */
+
+typedef struct _GIdleData GIdleData;
+typedef struct _GTimeoutData GTimeoutData;
+typedef struct _GSource GSource;
+typedef struct _GPollRec GPollRec;
+
+typedef enum {
+ G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
+ G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
+} GSourceFlags;
+
+struct _GSource {
+ GHook hook;
+ gint priority;
+ gpointer source_data;
+};
+
+struct _GMainLoop {
+ gboolean flag;
+};
+
+struct _GIdleData {
+ GSourceFunc callback;
+};
+
+struct _GTimeoutData {
+ GTimeVal expiration;
+ gint interval;
+ GSourceFunc callback;
+};
+
+struct _GPollRec {
+ gint priority;
+ GPollFD *fd;
+ GPollRec *next;
+};
+
+/* Forward declarations */
+
+static void g_main_poll (gint timeout,
+ gboolean use_priority,
+ gint priority);
+static gboolean g_timeout_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+static gboolean g_timeout_check (gpointer source_data,
+ GTimeVal *current_time);
+static gboolean g_timeout_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+static gboolean g_idle_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+static gboolean g_idle_check (gpointer source_data,
+ GTimeVal *current_time);
+static gboolean g_idle_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+
+/* Data */
+
+static GSList *pending_dispatches = NULL;
+static GHookList source_list = { 0 };
+
+static GSourceFuncs timeout_funcs = {
+ g_timeout_prepare,
+ g_timeout_check,
+ g_timeout_dispatch,
+ (GDestroyNotify)g_free
+};
+
+static GSourceFuncs idle_funcs = {
+ g_idle_prepare,
+ g_idle_check,
+ g_idle_dispatch,
+ (GDestroyNotify)g_free
+};
+
+#ifdef HAVE_POLL
+static GPollFunc poll_func = (GPollFunc)poll;
+#else
+
+/* The following implementation of poll() comes from the GNU C Library.
+ * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
+ */
+
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif /* HAVE_SYS_SELECT_H_ */
+
+#ifndef NO_FD_SET
+# define SELECT_MASK fd_set
+#else
+# ifndef _AIX
+typedef long fd_mask;
+# endif
+# if defined(_IBMR2)
+# define SELECT_MASK void
+# else
+# define SELECT_MASK int
+# endif
+#endif
+
+static gint
+g_poll (GPollFD *fds, guint nfds, gint timeout)
+{
+ struct timeval tv;
+ SELECT_MASK rset, wset, xset;
+ GPollFD *f;
+ int ready;
+ int maxfd = 0;
+
+ FD_ZERO (&rset);
+ FD_ZERO (&wset);
+ FD_ZERO (&xset);
+
+ for (f = fds; f < &fds[nfds]; ++f)
+ if (f->fd >= 0)
+ {
+ if (f->events & G_IO_IN)
+ FD_SET (f->fd, &rset);
+ if (f->events & G_IO_OUT)
+ FD_SET (f->fd, &wset);
+ if (f->events & G_IO_PRI)
+ FD_SET (f->fd, &xset);
+ if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
+ maxfd = f->fd;
+ }
+
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+
+ ready = select (maxfd + 1, &rset, &wset, &xset,
+ timeout == -1 ? NULL : &tv);
+ if (ready > 0)
+ for (f = fds; f < &fds[nfds]; ++f)
+ {
+ f->revents = 0;
+ if (f->fd >= 0)
+ {
+ if (FD_ISSET (f->fd, &rset))
+ f->revents |= G_IO_IN;
+ if (FD_ISSET (f->fd, &wset))
+ f->revents |= G_IO_OUT;
+ if (FD_ISSET (f->fd, &xset))
+ f->revents |= G_IO_PRI;
+ }
+ }
+
+ return ready;
+}
+
+static GPollFunc poll_func = g_poll;
+#endif
+
+/* Hooks for adding to the main loop */
+
+/* Use knowledge of insert_sorted algorithm here to make
+ * sure we insert at the end of equal priority items
+ */
+static gint
+g_source_compare (GHook *a, GHook *b)
+{
+ GSource *source_a = (GSource *)a;
+ GSource *source_b = (GSource *)b;
+
+ return (source_a->priority < source_b->priority) ? -1 : 1;
+}
+
+guint
+g_source_add (gint priority,
+ gboolean can_recurse,
+ GSourceFuncs *funcs,
+ gpointer source_data,
+ gpointer user_data,
+ GDestroyNotify notify)
+{
+ GSource *source;
+
+ if (!source_list.is_setup)
+ g_hook_list_init (&source_list, sizeof(GSource));
+
+ source = (GSource *)g_hook_alloc (&source_list);
+ source->priority = priority;
+ source->source_data = source_data;
+ source->hook.func = funcs;
+ source->hook.data = user_data;
+ source->hook.destroy = notify;
+
+ g_hook_insert_sorted (&source_list,
+ (GHook *)source,
+ g_source_compare);
+
+ if (can_recurse)
+ source->hook.flags |= G_SOURCE_CAN_RECURSE;
+
+ return source->hook.hook_id;
+}
+
+void
+g_source_remove (guint tag)
+{
+ GHook *hook = g_hook_get (&source_list, tag);
+ if (hook)
+ {
+ GSource *source = (GSource *)hook;
+ ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
+ g_hook_destroy_link (&source_list, hook);
+ }
+}
+
+void
+g_source_remove_by_user_data (gpointer user_data)
+{
+ GHook *hook = g_hook_find_data (&source_list, TRUE, user_data);
+ if (hook)
+ {
+ GSource *source = (GSource *)hook;
+ ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
+ g_hook_destroy_link (&source_list, hook);
+ }
+}
+
+static gboolean
+g_source_find_source_data (GHook *hook,
+ gpointer data)
+{
+ GSource *source = (GSource *)hook;
+ return (source->source_data == data);
+}
+
+void
+g_source_remove_by_source_data (gpointer source_data)
+{
+ GHook *hook = g_hook_find (&source_list, TRUE,
+ g_source_find_source_data, source_data);
+ if (hook)
+ {
+ GSource *source = (GSource *)hook;
+ ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
+ g_hook_destroy_link (&source_list, hook);
+ }
+}
+
+void g_get_current_time (GTimeVal *result)
+{
+ gettimeofday ((struct timeval *)result, NULL);
+}
+
+/* Running the main loop */
+
+static void
+g_main_dispatch (GTimeVal *current_time)
+{
+ while (pending_dispatches != NULL)
+ {
+ gboolean need_destroy;
+ GSource *source = pending_dispatches->data;
+ GSList *tmp_list;
+
+ tmp_list = pending_dispatches;
+ pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
+ g_slist_free_1 (tmp_list);
+
+ if (G_HOOK_IS_VALID (source))
+ {
+ source->hook.flags |= G_HOOK_FLAG_IN_CALL;
+ need_destroy = !((GSourceFuncs *)source->hook.func)->dispatch (source->source_data,
+ current_time,
+ source->hook.data);
+ source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
+
+ if (need_destroy)
+ g_hook_destroy_link (&source_list, (GHook *)source);
+ }
+
+ g_hook_unref (&source_list, (GHook *)source);
+ }
+}
+
+/* Run a single iteration of the mainloop, or, if !dispatch
+ * check to see if any events need dispatching, but don't
+ * run the loop.
+ */
+static gboolean
+g_main_iterate (gboolean block, gboolean dispatch)
+{
+ GHook *hook;
+ GTimeVal current_time;
+ gint nready = 0;
+ gint current_priority = 0;
+ gint timeout;
+
+ g_return_val_if_fail (!block || dispatch, FALSE);
+
+ g_get_current_time (&current_time);
+
+ /* If recursing, finish up current dispatch, before starting over */
+ if (pending_dispatches)
+ {
+ if (dispatch)
+ g_main_dispatch (&current_time);
+
+ return TRUE;
+ }
+
+ /* Prepare all sources */
+
+ timeout = block ? -1 : 0;
+
+ hook = g_hook_first_valid (&source_list, TRUE);
+ while (hook)
+ {
+ GSource *source = (GSource *)hook;
+ GHook *tmp;
+ gint source_timeout;
+
+ if ((nready > 0) && (source->priority > current_priority))
+ break;
+ if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
+ {
+ hook = g_hook_next_valid (hook, TRUE);
+ continue;
+ }
+
+ g_hook_ref (&source_list, hook);
+
+ if (((GSourceFuncs *)hook->func)->prepare (source->source_data,
+ &current_time,
+ &source_timeout))
+ {
+ if (!dispatch)
+ {
+ g_hook_unref (&source_list, hook);
+ return TRUE;
+ }
+ else
+ {
+ hook->flags |= G_SOURCE_READY;
+ nready++;
+ current_priority = source->priority;
+ timeout = 0;
+ }
+ }
+
+ if (source_timeout >= 0)
+ {
+ if (timeout < 0)
+ timeout = source_timeout;
+ else
+ timeout = MIN (timeout, source_timeout);
+ }
+
+ tmp = g_hook_next_valid (hook, TRUE);
+
+ g_hook_unref (&source_list, hook);
+ hook = tmp;
+ }
+
+ /* poll(), if necessary */
+
+ g_main_poll (timeout, nready > 0, current_priority);
+
+ /* Check to see what sources need to be dispatched */
+
+ nready = 0;
+
+ hook = g_hook_first_valid (&source_list, TRUE);
+ while (hook)
+ {
+ GSource *source = (GSource *)hook;
+ GHook *tmp;
+
+ if ((nready > 0) && (source->priority > current_priority))
+ break;
+ if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
+ {
+ hook = g_hook_next_valid (hook, TRUE);
+ continue;
+ }
+
+ g_hook_ref (&source_list, hook);
+
+ if ((hook->flags & G_SOURCE_READY) ||
+ ((GSourceFuncs *)hook->func)->check (source->source_data,
+ &current_time))
+ {
+ if (dispatch)
+ {
+ hook->flags &= ~G_SOURCE_READY;
+ g_hook_ref (&source_list, hook);
+ pending_dispatches = g_slist_prepend (pending_dispatches, source);
+ current_priority = source->priority;
+ nready++;
+ }
+ else
+ {
+ g_hook_unref (&source_list, hook);
+ return TRUE;
+ }
+ }
+
+ tmp = g_hook_next_valid (hook, TRUE);
+
+ g_hook_unref (&source_list, hook);
+ hook = tmp;
+ }
+
+ /* Now invoke the callbacks */
+
+ if (pending_dispatches)
+ {
+ pending_dispatches = g_slist_reverse (pending_dispatches);
+ g_main_dispatch (&current_time);
+ return TRUE;
+ }
+ else
+ return FALSE;
+}
+
+/* See if any events are pending
+ */
+gboolean
+g_main_pending ()
+{
+ return g_main_iterate (FALSE, FALSE);
+}
+
+/* Run a single iteration of the mainloop. If block is FALSE,
+ * will never block
+ */
+gboolean
+g_main_iteration (gboolean block)
+{
+ return g_main_iterate (block, TRUE);
+}
+
+GMainLoop *
+g_main_new ()
+{
+ GMainLoop *result = g_new (GMainLoop, 1);
+ result->flag = FALSE;
+
+ return result;
+}
+
+void
+g_main_run (GMainLoop *loop)
+{
+ loop->flag = FALSE;
+ while (!loop->flag)
+ g_main_iterate (TRUE, TRUE);
+}
+
+void
+g_main_quit (GMainLoop *loop)
+{
+ loop->flag = TRUE;
+}
+
+void
+g_main_destroy (GMainLoop *loop)
+{
+ g_free (loop);
+}
+
+static GPollRec *poll_records = NULL;
+static GPollRec *poll_free_list = NULL;
+static GMemChunk *poll_chunk;
+static guint n_poll_records = 0;
+
+static void
+g_main_poll (gint timeout, gboolean use_priority, gint priority)
+{
+ GPollFD *fd_array = g_new (GPollFD, n_poll_records);
+ GPollRec *pollrec;
+
+ gint i;
+ gint npoll;
+
+ pollrec = poll_records;
+ i = 0;
+ while (pollrec && (!use_priority || priority >= pollrec->priority))
+ {
+ fd_array[i].fd = pollrec->fd->fd;
+ fd_array[i].events = pollrec->fd->events;
+ fd_array[i].revents = 0;
+
+ pollrec = pollrec->next;
+ i++;
+ }
+
+ npoll = i;
+ (*poll_func) (fd_array, npoll, timeout);
+
+ pollrec = poll_records;
+ i = 0;
+ while (i < npoll)
+ {
+ pollrec->fd->revents = fd_array[i].revents;
+ pollrec = pollrec->next;
+ i++;
+ }
+
+ g_free (fd_array);
+}
+
+void
+g_main_poll_add (gint priority,
+ GPollFD *fd)
+{
+ GPollRec *lastrec, *pollrec, *newrec;
+
+ if (!poll_chunk)
+ poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
+
+ newrec = g_chunk_new (GPollRec, poll_chunk);
+ newrec->fd = fd;
+ newrec->priority = priority;
+
+ lastrec = NULL;
+ pollrec = poll_records;
+ while (pollrec && priority >= pollrec->priority)
+ {
+ lastrec = pollrec;
+ pollrec = pollrec->next;
+ }
+
+ if (lastrec)
+ lastrec->next = newrec;
+ else
+ poll_records = newrec;
+
+ newrec->next = pollrec;
+
+ n_poll_records++;
+}
+
+void
+g_main_poll_remove (GPollFD *fd)
+{
+ GPollRec *pollrec, *lastrec;
+
+ lastrec = NULL;
+ pollrec = poll_records;
+
+ while (pollrec)
+ {
+ if (pollrec->fd == fd)
+ {
+ if (lastrec != NULL)
+ lastrec->next = pollrec->next;
+ else
+ poll_records = pollrec->next;
+
+ pollrec->next = poll_free_list;
+ poll_free_list = pollrec;
+ }
+ lastrec = pollrec;
+ pollrec = pollrec->next;
+ }
+
+ n_poll_records--;
+}
+
+void
+g_main_set_poll_func (GPollFunc func)
+{
+ if (func)
+ poll_func = func;
+ else
+ poll_func = (GPollFunc)poll;
+}
+
+/* Timeouts */
+
+static gboolean
+g_timeout_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout)
+{
+ glong msec;
+ GTimeoutData *data = source_data;
+
+ msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
+ (data->expiration.tv_usec - current_time->tv_usec) / 1000;
+
+ *timeout = (msec <= 0) ? 0 : msec;
+
+ return (msec <= 0);
+}
+
+static gboolean
+g_timeout_check (gpointer source_data,
+ GTimeVal *current_time)
+{
+ GTimeoutData *data = source_data;
+
+ return (data->expiration.tv_sec < current_time->tv_sec) ||
+ ((data->expiration.tv_sec == current_time->tv_sec) &&
+ (data->expiration.tv_usec <= current_time->tv_usec));
+}
+
+static gboolean
+g_timeout_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data)
+{
+ GTimeoutData *data = source_data;
+
+ if (data->callback(user_data))
+ {
+ data->expiration.tv_sec = current_time->tv_sec;
+ data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
+ if (data->expiration.tv_usec >= 1000000)
+ {
+ data->expiration.tv_usec -= 1000000;
+ data->expiration.tv_sec++;
+ }
+ return TRUE;
+ }
+ else
+ return FALSE;
+}
+
+guint
+g_timeout_add_full (gint priority,
+ guint interval,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify notify)
+{
+ GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
+
+ timeout_data->interval = interval;
+ timeout_data->callback = function;
+ g_get_current_time (&timeout_data->expiration);
+
+ timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
+ if (timeout_data->expiration.tv_usec >= 1000000)
+ {
+ timeout_data->expiration.tv_usec -= 1000000;
+ timeout_data->expiration.tv_sec++;
+ }
+
+ return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
+}
+
+guint
+g_timeout_add (guint32 interval,
+ GSourceFunc function,
+ gpointer data)
+{
+ return g_timeout_add_full (0, interval, function, data, NULL);
+}
+
+/* Idle functions */
+
+static gboolean
+g_idle_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout)
+{
+ timeout = 0;
+ return TRUE;
+}
+
+static gboolean
+g_idle_check (gpointer source_data,
+ GTimeVal *current_time)
+{
+ return TRUE;
+}
+
+static gboolean
+g_idle_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data)
+{
+ GIdleData *data = source_data;
+
+ return (*data->callback)(user_data);
+}
+
+guint
+g_idle_add_full (gint priority,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify notify)
+{
+ GIdleData *idle_data = g_new (GIdleData, 1);
+
+ idle_data->callback = function;
+
+ return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
+}
+
+guint
+g_idle_add (GSourceFunc function,
+ gpointer data)
+{
+ return g_idle_add_full (0, function, data, NULL);
+}
diff --git a/glib/gutils.c b/glib/gutils.c
index bc4c452fd..cc6594937 100644
--- a/glib/gutils.c
+++ b/glib/gutils.c
@@ -537,6 +537,8 @@ g_int_hash (gconstpointer v)
return *(const gint*) v;
}
+#if 0 /* Old IO Channels */
+
GIOChannel*
g_iochannel_new (gint fd)
{
@@ -596,6 +598,7 @@ g_iochannel_wakeup_peer (GIOChannel *channel)
#endif /* NATIVE_WIN32 */
}
+#endif /* Old IO Channels */
#ifdef NATIVE_WIN32
#ifdef _MSC_VER
diff --git a/glist.c b/glist.c
index ab69826a2..41a09dd18 100644
--- a/glist.c
+++ b/glist.c
@@ -574,3 +574,62 @@ g_list_sort (GList *list,
g_list_sort (l2, compare_func),
compare_func);
}
+
+GList*
+g_list_sort2 (GList *list,
+ GCompareFunc compare_func)
+{
+ GSList *runs = NULL;
+ GList *tmp;
+
+ /* Degenerate case. */
+ if (!list) return NULL;
+
+ /* Assume: list = [12,2,4,11,2,4,6,1,1,12]. */
+ for (tmp = list; tmp; )
+ {
+ GList *tmp2;
+ for (tmp2 = tmp;
+ tmp2->next && compare_func (tmp2->data, tmp2->next->data) <= 0;
+ tmp2 = tmp2->next)
+ /* Nothing */;
+ runs = g_slist_append (runs, tmp);
+ tmp = tmp2->next;
+ tmp2->next = NULL;
+ }
+ /* Now: runs = [[12],[2,4,11],[2,4,6],[1,1,12]]. */
+
+ while (runs->next)
+ {
+ /* We have more than one run. Merge pairwise. */
+ GSList *dst, *src, *dstprev = NULL;
+ dst = src = runs;
+ while (src && src->next)
+ {
+ dst->data = g_list_sort_merge (src->data,
+ src->next->data,
+ compare_func);
+ dstprev = dst;
+ dst = dst->next;
+ src = src->next->next;
+ }
+
+ /* If number of runs was odd, just keep the last. */
+ if (src)
+ {
+ dst->data = src->data;
+ dstprev = dst;
+ dst = dst->next;
+ }
+
+ dstprev->next = NULL;
+ g_slist_free (dst);
+ }
+
+ /* After 1st loop: runs = [[2,4,11,12],[1,1,2,4,6,12]]. */
+ /* After 2nd loop: runs = [[1,1,2,2,4,4,6,11,12,12]]. */
+
+ list = runs->data;
+ g_slist_free (runs);
+ return list;
+}
diff --git a/gmain.c b/gmain.c
new file mode 100644
index 000000000..79c1191f1
--- /dev/null
+++ b/gmain.c
@@ -0,0 +1,731 @@
+/* GLIB - Library of useful routines for C programming
+ * Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
+ *
+ * gmain.c: Main loop abstraction, timeouts, and idle functions
+ * Copyright 1998 Owen Taylor
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Library General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Library General Public License for more details.
+ *
+ * You should have received a copy of the GNU Library General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ * Boston, MA 02111-1307, USA.
+ */
+
+#include "glib.h"
+#include <sys/time.h>
+#include <unistd.h>
+#include "config.h"
+
+/* Types */
+
+typedef struct _GIdleData GIdleData;
+typedef struct _GTimeoutData GTimeoutData;
+typedef struct _GSource GSource;
+typedef struct _GPollRec GPollRec;
+
+typedef enum {
+ G_SOURCE_READY = 1 << G_HOOK_FLAG_USER_SHIFT,
+ G_SOURCE_CAN_RECURSE = 1 << (G_HOOK_FLAG_USER_SHIFT + 1)
+} GSourceFlags;
+
+struct _GSource {
+ GHook hook;
+ gint priority;
+ gpointer source_data;
+};
+
+struct _GMainLoop {
+ gboolean flag;
+};
+
+struct _GIdleData {
+ GSourceFunc callback;
+};
+
+struct _GTimeoutData {
+ GTimeVal expiration;
+ gint interval;
+ GSourceFunc callback;
+};
+
+struct _GPollRec {
+ gint priority;
+ GPollFD *fd;
+ GPollRec *next;
+};
+
+/* Forward declarations */
+
+static void g_main_poll (gint timeout,
+ gboolean use_priority,
+ gint priority);
+static gboolean g_timeout_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+static gboolean g_timeout_check (gpointer source_data,
+ GTimeVal *current_time);
+static gboolean g_timeout_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+static gboolean g_idle_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout);
+static gboolean g_idle_check (gpointer source_data,
+ GTimeVal *current_time);
+static gboolean g_idle_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data);
+
+/* Data */
+
+static GSList *pending_dispatches = NULL;
+static GHookList source_list = { 0 };
+
+static GSourceFuncs timeout_funcs = {
+ g_timeout_prepare,
+ g_timeout_check,
+ g_timeout_dispatch,
+ (GDestroyNotify)g_free
+};
+
+static GSourceFuncs idle_funcs = {
+ g_idle_prepare,
+ g_idle_check,
+ g_idle_dispatch,
+ (GDestroyNotify)g_free
+};
+
+#ifdef HAVE_POLL
+static GPollFunc poll_func = (GPollFunc)poll;
+#else
+
+/* The following implementation of poll() comes from the GNU C Library.
+ * Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
+ */
+
+#ifdef HAVE_SYS_SELECT_H
+#include <sys/select.h>
+#endif /* HAVE_SYS_SELECT_H_ */
+
+#ifndef NO_FD_SET
+# define SELECT_MASK fd_set
+#else
+# ifndef _AIX
+typedef long fd_mask;
+# endif
+# if defined(_IBMR2)
+# define SELECT_MASK void
+# else
+# define SELECT_MASK int
+# endif
+#endif
+
+static gint
+g_poll (GPollFD *fds, guint nfds, gint timeout)
+{
+ struct timeval tv;
+ SELECT_MASK rset, wset, xset;
+ GPollFD *f;
+ int ready;
+ int maxfd = 0;
+
+ FD_ZERO (&rset);
+ FD_ZERO (&wset);
+ FD_ZERO (&xset);
+
+ for (f = fds; f < &fds[nfds]; ++f)
+ if (f->fd >= 0)
+ {
+ if (f->events & G_IO_IN)
+ FD_SET (f->fd, &rset);
+ if (f->events & G_IO_OUT)
+ FD_SET (f->fd, &wset);
+ if (f->events & G_IO_PRI)
+ FD_SET (f->fd, &xset);
+ if (f->fd > maxfd && (f->events & (POLLIN|POLLOUT|POLLPRI)))
+ maxfd = f->fd;
+ }
+
+ tv.tv_sec = timeout / 1000;
+ tv.tv_usec = (timeout % 1000) * 1000;
+
+ ready = select (maxfd + 1, &rset, &wset, &xset,
+ timeout == -1 ? NULL : &tv);
+ if (ready > 0)
+ for (f = fds; f < &fds[nfds]; ++f)
+ {
+ f->revents = 0;
+ if (f->fd >= 0)
+ {
+ if (FD_ISSET (f->fd, &rset))
+ f->revents |= G_IO_IN;
+ if (FD_ISSET (f->fd, &wset))
+ f->revents |= G_IO_OUT;
+ if (FD_ISSET (f->fd, &xset))
+ f->revents |= G_IO_PRI;
+ }
+ }
+
+ return ready;
+}
+
+static GPollFunc poll_func = g_poll;
+#endif
+
+/* Hooks for adding to the main loop */
+
+/* Use knowledge of insert_sorted algorithm here to make
+ * sure we insert at the end of equal priority items
+ */
+static gint
+g_source_compare (GHook *a, GHook *b)
+{
+ GSource *source_a = (GSource *)a;
+ GSource *source_b = (GSource *)b;
+
+ return (source_a->priority < source_b->priority) ? -1 : 1;
+}
+
+guint
+g_source_add (gint priority,
+ gboolean can_recurse,
+ GSourceFuncs *funcs,
+ gpointer source_data,
+ gpointer user_data,
+ GDestroyNotify notify)
+{
+ GSource *source;
+
+ if (!source_list.is_setup)
+ g_hook_list_init (&source_list, sizeof(GSource));
+
+ source = (GSource *)g_hook_alloc (&source_list);
+ source->priority = priority;
+ source->source_data = source_data;
+ source->hook.func = funcs;
+ source->hook.data = user_data;
+ source->hook.destroy = notify;
+
+ g_hook_insert_sorted (&source_list,
+ (GHook *)source,
+ g_source_compare);
+
+ if (can_recurse)
+ source->hook.flags |= G_SOURCE_CAN_RECURSE;
+
+ return source->hook.hook_id;
+}
+
+void
+g_source_remove (guint tag)
+{
+ GHook *hook = g_hook_get (&source_list, tag);
+ if (hook)
+ {
+ GSource *source = (GSource *)hook;
+ ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
+ g_hook_destroy_link (&source_list, hook);
+ }
+}
+
+void
+g_source_remove_by_user_data (gpointer user_data)
+{
+ GHook *hook = g_hook_find_data (&source_list, TRUE, user_data);
+ if (hook)
+ {
+ GSource *source = (GSource *)hook;
+ ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
+ g_hook_destroy_link (&source_list, hook);
+ }
+}
+
+static gboolean
+g_source_find_source_data (GHook *hook,
+ gpointer data)
+{
+ GSource *source = (GSource *)hook;
+ return (source->source_data == data);
+}
+
+void
+g_source_remove_by_source_data (gpointer source_data)
+{
+ GHook *hook = g_hook_find (&source_list, TRUE,
+ g_source_find_source_data, source_data);
+ if (hook)
+ {
+ GSource *source = (GSource *)hook;
+ ((GSourceFuncs *)source->hook.func)->destroy (source->source_data);
+ g_hook_destroy_link (&source_list, hook);
+ }
+}
+
+void g_get_current_time (GTimeVal *result)
+{
+ gettimeofday ((struct timeval *)result, NULL);
+}
+
+/* Running the main loop */
+
+static void
+g_main_dispatch (GTimeVal *current_time)
+{
+ while (pending_dispatches != NULL)
+ {
+ gboolean need_destroy;
+ GSource *source = pending_dispatches->data;
+ GSList *tmp_list;
+
+ tmp_list = pending_dispatches;
+ pending_dispatches = g_slist_remove_link (pending_dispatches, pending_dispatches);
+ g_slist_free_1 (tmp_list);
+
+ if (G_HOOK_IS_VALID (source))
+ {
+ source->hook.flags |= G_HOOK_FLAG_IN_CALL;
+ need_destroy = !((GSourceFuncs *)source->hook.func)->dispatch (source->source_data,
+ current_time,
+ source->hook.data);
+ source->hook.flags &= ~G_HOOK_FLAG_IN_CALL;
+
+ if (need_destroy)
+ g_hook_destroy_link (&source_list, (GHook *)source);
+ }
+
+ g_hook_unref (&source_list, (GHook *)source);
+ }
+}
+
+/* Run a single iteration of the mainloop, or, if !dispatch
+ * check to see if any events need dispatching, but don't
+ * run the loop.
+ */
+static gboolean
+g_main_iterate (gboolean block, gboolean dispatch)
+{
+ GHook *hook;
+ GTimeVal current_time;
+ gint nready = 0;
+ gint current_priority = 0;
+ gint timeout;
+
+ g_return_val_if_fail (!block || dispatch, FALSE);
+
+ g_get_current_time (&current_time);
+
+ /* If recursing, finish up current dispatch, before starting over */
+ if (pending_dispatches)
+ {
+ if (dispatch)
+ g_main_dispatch (&current_time);
+
+ return TRUE;
+ }
+
+ /* Prepare all sources */
+
+ timeout = block ? -1 : 0;
+
+ hook = g_hook_first_valid (&source_list, TRUE);
+ while (hook)
+ {
+ GSource *source = (GSource *)hook;
+ GHook *tmp;
+ gint source_timeout;
+
+ if ((nready > 0) && (source->priority > current_priority))
+ break;
+ if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
+ {
+ hook = g_hook_next_valid (hook, TRUE);
+ continue;
+ }
+
+ g_hook_ref (&source_list, hook);
+
+ if (((GSourceFuncs *)hook->func)->prepare (source->source_data,
+ &current_time,
+ &source_timeout))
+ {
+ if (!dispatch)
+ {
+ g_hook_unref (&source_list, hook);
+ return TRUE;
+ }
+ else
+ {
+ hook->flags |= G_SOURCE_READY;
+ nready++;
+ current_priority = source->priority;
+ timeout = 0;
+ }
+ }
+
+ if (source_timeout >= 0)
+ {
+ if (timeout < 0)
+ timeout = source_timeout;
+ else
+ timeout = MIN (timeout, source_timeout);
+ }
+
+ tmp = g_hook_next_valid (hook, TRUE);
+
+ g_hook_unref (&source_list, hook);
+ hook = tmp;
+ }
+
+ /* poll(), if necessary */
+
+ g_main_poll (timeout, nready > 0, current_priority);
+
+ /* Check to see what sources need to be dispatched */
+
+ nready = 0;
+
+ hook = g_hook_first_valid (&source_list, TRUE);
+ while (hook)
+ {
+ GSource *source = (GSource *)hook;
+ GHook *tmp;
+
+ if ((nready > 0) && (source->priority > current_priority))
+ break;
+ if (!(hook->flags & G_SOURCE_CAN_RECURSE) && G_HOOK_IN_CALL (hook))
+ {
+ hook = g_hook_next_valid (hook, TRUE);
+ continue;
+ }
+
+ g_hook_ref (&source_list, hook);
+
+ if ((hook->flags & G_SOURCE_READY) ||
+ ((GSourceFuncs *)hook->func)->check (source->source_data,
+ &current_time))
+ {
+ if (dispatch)
+ {
+ hook->flags &= ~G_SOURCE_READY;
+ g_hook_ref (&source_list, hook);
+ pending_dispatches = g_slist_prepend (pending_dispatches, source);
+ current_priority = source->priority;
+ nready++;
+ }
+ else
+ {
+ g_hook_unref (&source_list, hook);
+ return TRUE;
+ }
+ }
+
+ tmp = g_hook_next_valid (hook, TRUE);
+
+ g_hook_unref (&source_list, hook);
+ hook = tmp;
+ }
+
+ /* Now invoke the callbacks */
+
+ if (pending_dispatches)
+ {
+ pending_dispatches = g_slist_reverse (pending_dispatches);
+ g_main_dispatch (&current_time);
+ return TRUE;
+ }
+ else
+ return FALSE;
+}
+
+/* See if any events are pending
+ */
+gboolean
+g_main_pending ()
+{
+ return g_main_iterate (FALSE, FALSE);
+}
+
+/* Run a single iteration of the mainloop. If block is FALSE,
+ * will never block
+ */
+gboolean
+g_main_iteration (gboolean block)
+{
+ return g_main_iterate (block, TRUE);
+}
+
+GMainLoop *
+g_main_new ()
+{
+ GMainLoop *result = g_new (GMainLoop, 1);
+ result->flag = FALSE;
+
+ return result;
+}
+
+void
+g_main_run (GMainLoop *loop)
+{
+ loop->flag = FALSE;
+ while (!loop->flag)
+ g_main_iterate (TRUE, TRUE);
+}
+
+void
+g_main_quit (GMainLoop *loop)
+{
+ loop->flag = TRUE;
+}
+
+void
+g_main_destroy (GMainLoop *loop)
+{
+ g_free (loop);
+}
+
+static GPollRec *poll_records = NULL;
+static GPollRec *poll_free_list = NULL;
+static GMemChunk *poll_chunk;
+static guint n_poll_records = 0;
+
+static void
+g_main_poll (gint timeout, gboolean use_priority, gint priority)
+{
+ GPollFD *fd_array = g_new (GPollFD, n_poll_records);
+ GPollRec *pollrec;
+
+ gint i;
+ gint npoll;
+
+ pollrec = poll_records;
+ i = 0;
+ while (pollrec && (!use_priority || priority >= pollrec->priority))
+ {
+ fd_array[i].fd = pollrec->fd->fd;
+ fd_array[i].events = pollrec->fd->events;
+ fd_array[i].revents = 0;
+
+ pollrec = pollrec->next;
+ i++;
+ }
+
+ npoll = i;
+ (*poll_func) (fd_array, npoll, timeout);
+
+ pollrec = poll_records;
+ i = 0;
+ while (i < npoll)
+ {
+ pollrec->fd->revents = fd_array[i].revents;
+ pollrec = pollrec->next;
+ i++;
+ }
+
+ g_free (fd_array);
+}
+
+void
+g_main_poll_add (gint priority,
+ GPollFD *fd)
+{
+ GPollRec *lastrec, *pollrec, *newrec;
+
+ if (!poll_chunk)
+ poll_chunk = g_mem_chunk_create (GPollRec, 32, G_ALLOC_ONLY);
+
+ newrec = g_chunk_new (GPollRec, poll_chunk);
+ newrec->fd = fd;
+ newrec->priority = priority;
+
+ lastrec = NULL;
+ pollrec = poll_records;
+ while (pollrec && priority >= pollrec->priority)
+ {
+ lastrec = pollrec;
+ pollrec = pollrec->next;
+ }
+
+ if (lastrec)
+ lastrec->next = newrec;
+ else
+ poll_records = newrec;
+
+ newrec->next = pollrec;
+
+ n_poll_records++;
+}
+
+void
+g_main_poll_remove (GPollFD *fd)
+{
+ GPollRec *pollrec, *lastrec;
+
+ lastrec = NULL;
+ pollrec = poll_records;
+
+ while (pollrec)
+ {
+ if (pollrec->fd == fd)
+ {
+ if (lastrec != NULL)
+ lastrec->next = pollrec->next;
+ else
+ poll_records = pollrec->next;
+
+ pollrec->next = poll_free_list;
+ poll_free_list = pollrec;
+ }
+ lastrec = pollrec;
+ pollrec = pollrec->next;
+ }
+
+ n_poll_records--;
+}
+
+void
+g_main_set_poll_func (GPollFunc func)
+{
+ if (func)
+ poll_func = func;
+ else
+ poll_func = (GPollFunc)poll;
+}
+
+/* Timeouts */
+
+static gboolean
+g_timeout_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout)
+{
+ glong msec;
+ GTimeoutData *data = source_data;
+
+ msec = (data->expiration.tv_sec - current_time->tv_sec) * 1000 +
+ (data->expiration.tv_usec - current_time->tv_usec) / 1000;
+
+ *timeout = (msec <= 0) ? 0 : msec;
+
+ return (msec <= 0);
+}
+
+static gboolean
+g_timeout_check (gpointer source_data,
+ GTimeVal *current_time)
+{
+ GTimeoutData *data = source_data;
+
+ return (data->expiration.tv_sec < current_time->tv_sec) ||
+ ((data->expiration.tv_sec == current_time->tv_sec) &&
+ (data->expiration.tv_usec <= current_time->tv_usec));
+}
+
+static gboolean
+g_timeout_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data)
+{
+ GTimeoutData *data = source_data;
+
+ if (data->callback(user_data))
+ {
+ data->expiration.tv_sec = current_time->tv_sec;
+ data->expiration.tv_usec = current_time->tv_usec + data->interval * 1000;
+ if (data->expiration.tv_usec >= 1000000)
+ {
+ data->expiration.tv_usec -= 1000000;
+ data->expiration.tv_sec++;
+ }
+ return TRUE;
+ }
+ else
+ return FALSE;
+}
+
+guint
+g_timeout_add_full (gint priority,
+ guint interval,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify notify)
+{
+ GTimeoutData *timeout_data = g_new (GTimeoutData, 1);
+
+ timeout_data->interval = interval;
+ timeout_data->callback = function;
+ g_get_current_time (&timeout_data->expiration);
+
+ timeout_data->expiration.tv_usec += timeout_data->interval * 1000;
+ if (timeout_data->expiration.tv_usec >= 1000000)
+ {
+ timeout_data->expiration.tv_usec -= 1000000;
+ timeout_data->expiration.tv_sec++;
+ }
+
+ return g_source_add (priority, FALSE, &timeout_funcs, timeout_data, data, notify);
+}
+
+guint
+g_timeout_add (guint32 interval,
+ GSourceFunc function,
+ gpointer data)
+{
+ return g_timeout_add_full (0, interval, function, data, NULL);
+}
+
+/* Idle functions */
+
+static gboolean
+g_idle_prepare (gpointer source_data,
+ GTimeVal *current_time,
+ gint *timeout)
+{
+ timeout = 0;
+ return TRUE;
+}
+
+static gboolean
+g_idle_check (gpointer source_data,
+ GTimeVal *current_time)
+{
+ return TRUE;
+}
+
+static gboolean
+g_idle_dispatch (gpointer source_data,
+ GTimeVal *current_time,
+ gpointer user_data)
+{
+ GIdleData *data = source_data;
+
+ return (*data->callback)(user_data);
+}
+
+guint
+g_idle_add_full (gint priority,
+ GSourceFunc function,
+ gpointer data,
+ GDestroyNotify notify)
+{
+ GIdleData *idle_data = g_new (GIdleData, 1);
+
+ idle_data->callback = function;
+
+ return g_source_add (priority, FALSE, &idle_funcs, idle_data, data, notify);
+}
+
+guint
+g_idle_add (GSourceFunc function,
+ gpointer data)
+{
+ return g_idle_add_full (0, function, data, NULL);
+}
diff --git a/gutils.c b/gutils.c
index bc4c452fd..cc6594937 100644
--- a/gutils.c
+++ b/gutils.c
@@ -537,6 +537,8 @@ g_int_hash (gconstpointer v)
return *(const gint*) v;
}
+#if 0 /* Old IO Channels */
+
GIOChannel*
g_iochannel_new (gint fd)
{
@@ -596,6 +598,7 @@ g_iochannel_wakeup_peer (GIOChannel *channel)
#endif /* NATIVE_WIN32 */
}
+#endif /* Old IO Channels */
#ifdef NATIVE_WIN32
#ifdef _MSC_VER