[Xfce4-commits] <glib-objc:no-foundation-dep> add wrappers for GMainContext and GMainLoop

Brian J. Tarricone noreply at xfce.org
Sun Nov 22 04:02:11 CET 2009


Updating branch refs/heads/no-foundation-dep
         to d921853cce5748585fda21b96a51ec75716736ba (commit)
       from 29836ebf0aa99e27c66df659d9801153caa8eca2 (commit)

commit d921853cce5748585fda21b96a51ec75716736ba
Author: Brian J. Tarricone <brian at tarricone.org>
Date:   Tue Jul 7 01:27:25 2009 -0700

    add wrappers for GMainContext and GMainLoop

 glib-objc/GOCMain.h   |   90 +++++++++++++++++
 glib-objc/GOCMain.m   |  266 +++++++++++++++++++++++++++++++++++++++++++++++++
 glib-objc/Makefile.am |    3 +
 glib-objc/glib-objc.h |    1 +
 4 files changed, 360 insertions(+), 0 deletions(-)

diff --git a/glib-objc/GOCMain.h b/glib-objc/GOCMain.h
new file mode 100644
index 0000000..3cdc548
--- /dev/null
+++ b/glib-objc/GOCMain.h
@@ -0,0 +1,90 @@
+/*
+ *  glib-objc - objective-c bindings for glib/gobject
+ *
+ *  Copyright (c) 2009 Brian Tarricone <brian at tarricone.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU Lesser General Public License as published
+ *  by the Free Software Foundation; version 2 of the License ONLY.
+ *
+ *  This program 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 Lesser General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifndef __GOC_MAIN_LOOP_H__
+#define __GOC_MAIN_LOOP_H__
+
+#include <glib.h>
+
+#import <glib-objc/GOCObjectBase.h>
+
+typedef struct _GOCMainContextPriv  GOCMainContextPriv;
+typedef struct _GOCMainLoopPriv     GOCMainLoopPriv;
+
+ at interface GOCMainContext : GOCObjectBase
+{
+  @private
+    GOCMainContextPriv *priv;
+}
+
++ (id)defaultContext;
+
+- (BOOL)doIteration:(BOOL)mayBlock;
+- (BOOL)eventsPending;
+
+- (unsigned int)addTimeout:(unsigned int)interval
+              withCallback:(GSourceFunc)function
+                   andData:(void *)data;
+- (unsigned int)addTimeout:(unsigned int)interval
+              withCallback:(GSourceFunc)function
+                   andData:(void *)data
+        andDestroyNotifier:(GDestroyNotify)notify
+                atPriority:(int)priority;
+
+- (unsigned int)addTimeoutSeconds:(unsigned int)interval
+                     withCallback:(GSourceFunc)function
+                          andData:(void *)data;
+- (unsigned int)addTimeoutSeconds:(unsigned int)interval
+                     withCallback:(GSourceFunc)function
+                          andData:(void *)data
+               andDestroyNotifier:(GDestroyNotify)notify
+                       atPriority:(int)priority;
+
+- (unsigned int)addIdleCallback:(GSourceFunc)function
+                       withData:(void *)data;
+- (unsigned int)addIdleCallback:(GSourceFunc)function
+                       withData:(void *)data
+             andDestroyNotifier:(GDestroyNotify)notify
+                     atPriority:(int)priority;
+
+- (BOOL)removeSourceById:(unsigned int)sourceId;
+
+ at end
+
+
+ at interface GOCMainLoop : GOCObjectBase
+{
+  @private
+    GOCMainLoopPriv *priv;
+}
+
+- (id)init;
+- (id)initWithContext:(GOCMainContext *)context;
+- (id)initWithContext:(GOCMainContext *)context
+            isRunning:(BOOL)running;
+
+- (void)run;
+- (BOOL)running;
+- (void)quit;
+
+- (GOCMainContext *)context;
+
+ at end
+
+#endif  /* __GOC_MAIN_LOOP_H__ */
diff --git a/glib-objc/GOCMain.m b/glib-objc/GOCMain.m
new file mode 100644
index 0000000..56b510c
--- /dev/null
+++ b/glib-objc/GOCMain.m
@@ -0,0 +1,266 @@
+/*
+ *  glib-objc - objective-c bindings for glib/gobject
+ *
+ *  Copyright (c) 2009 Brian Tarricone <brian at tarricone.org>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU Lesser General Public License as published
+ *  by the Free Software Foundation; version 2 of the License ONLY.
+ *
+ *  This program 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 Lesser General Public License
+ *  along with this program; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#import "GOCMain.h"
+
+#include "goc-private.h"
+
+struct _GOCMainContextPriv
+{
+    GMainContext *ctx_ptr;
+};
+
+ at implementation GOCMainContext
+
+- (id)init
+{
+    self = [super init];
+    if(self) {
+        priv = g_slice_new0(GOCMainContextPriv);
+        priv->ctx_ptr = g_main_context_new();
+    }
+    return self;
+}
+
+
+/*< private >*/
+- (id)initWithGMainContext:(GMainContext *)mainContext
+{
+    self = [super init];
+    if(self) {
+        priv = g_slice_new0(GOCMainContextPriv);
+        priv->ctx_ptr = mainContext;
+    }
+    return self;
+}
+
+static GOnce default_context_once = G_ONCE_INIT;
+static GOCMainContext *default_context = NULL;
+
+static gpointer
+default_context_init(gpointer data)
+{
+    default_context = [[GOCMainContext alloc] initWithGMainContext:g_main_context_default()];
+    return NULL;
+}
+
++ (id)defaultContext
+{
+    g_once(&default_context_once, default_context_init, NULL);
+    return default_context;
+}
+
+- (BOOL)doIteration:(BOOL)mayBlock
+{
+    return g_main_context_iteration(priv->ctx_ptr, mayBlock);
+}
+
+- (BOOL)eventsPending
+{
+    return g_main_context_pending(priv->ctx_ptr);
+}
+
+- (unsigned int)addTimeout:(unsigned int)interval
+              withCallback:(GSourceFunc)function
+                   andData:(void *)data
+{
+    return [self addTimeout:interval
+               withCallback:function
+                    andData:data
+         andDestroyNotifier:NULL
+                 atPriority:G_PRIORITY_DEFAULT];
+}
+
+- (unsigned int)addTimeout:(unsigned int)interval
+              withCallback:(GSourceFunc)function
+                   andData:(void *)data
+        andDestroyNotifier:(GDestroyNotify)notify
+                atPriority:(int)priority
+{
+    if(self == default_context)
+        return g_timeout_add_full(priority, interval, function, data, notify);
+    else {
+        guint sid;
+        GSource *source = g_timeout_source_new(interval);
+        g_source_set_priority(source, priority);
+        g_source_set_callback(source, function, data, notify);
+        sid = g_source_attach(source, priv->ctx_ptr);
+        g_source_unref(source);
+        return sid;
+    }
+}
+
+- (unsigned int)addTimeoutSeconds:(unsigned int)interval
+                     withCallback:(GSourceFunc)function
+                          andData:(void *)data
+{
+    return [self addTimeoutSeconds:interval
+                      withCallback:function
+                           andData:data
+                andDestroyNotifier:NULL
+                        atPriority:G_PRIORITY_DEFAULT];
+}
+
+- (unsigned int)addTimeoutSeconds:(unsigned int)interval
+                     withCallback:(GSourceFunc)function
+                          andData:(void *)data
+               andDestroyNotifier:(GDestroyNotify)notify
+                       atPriority:(int)priority
+{
+    if(self == default_context)
+        return g_timeout_add_seconds_full(priority, interval, function, data, notify);
+    else {
+        guint sid;
+        GSource *source = g_timeout_source_new_seconds(interval);
+        g_source_set_priority(source, priority);
+        g_source_set_callback(source, function, data, notify);
+        sid = g_source_attach(source, priv->ctx_ptr);
+        g_source_unref(source);
+        return sid;
+    }
+}
+
+- (unsigned int)addIdleCallback:(GSourceFunc)function
+                       withData:(void *)data
+{
+    return [self addIdleCallback:function
+                        withData:data
+              andDestroyNotifier:NULL
+                      atPriority:G_PRIORITY_DEFAULT_IDLE];
+}
+
+- (unsigned int)addIdleCallback:(GSourceFunc)function
+                       withData:(void *)data
+             andDestroyNotifier:(GDestroyNotify)notify
+                     atPriority:(int)priority
+{
+    if(self == default_context)
+        return g_idle_add_full(priority, function, data, notify);
+    else {
+        guint sid;
+        GSource *source = g_idle_source_new();
+        g_source_set_priority(source, priority);
+        g_source_set_callback(source, function, data, notify);
+        sid = g_source_attach(source, priv->ctx_ptr);
+        g_source_unref(source);
+        return sid;
+    }
+}
+
+- (BOOL)removeSourceById:(unsigned int)sourceId
+{
+    if(self == default_context)
+        return g_source_remove(sourceId);
+    else {
+        GSource *source = g_main_context_find_source_by_id(priv->ctx_ptr, sourceId);
+        if(!source)
+            return NO;
+        g_source_destroy(source);
+        return YES;
+    }
+}
+
+- (void)free
+{
+    _goc_return_if_fail(self != default_context);
+
+    g_main_context_unref(priv->ctx_ptr);
+    g_slice_free(GOCMainContextPriv, priv);
+
+    [super free];
+}
+
+/*< private >*/
+- (GMainContext *)_peekGMainContext
+{
+    return priv->ctx_ptr;
+}
+
+ at end
+
+
+
+struct _GOCMainLoopPriv
+{
+    GOCMainContext *ctx;
+    GMainLoop *loop_ptr;
+};
+
+ at implementation GOCMainLoop
+
+- (id)init
+{
+    return [self initWithContext:NULL isRunning:NO];
+}
+
+- (id)initWithContext:(GOCMainContext *)context
+{
+    return [self initWithContext:context isRunning:NO];
+}
+
+- (id)initWithContext:(GOCMainContext *)context
+            isRunning:(BOOL)running
+{
+    self = [super init];
+    if(self) {
+        priv = g_slice_new0(GOCMainLoopPriv);
+
+        if(!context)
+            context = [GOCMainContext defaultContext];
+
+        priv->ctx = [context ref];
+        priv->loop_ptr = g_main_loop_new([context _peekGMainContext], running);
+    }
+    return self;
+}
+
+- (void)run
+{
+    g_main_loop_run(priv->loop_ptr);
+}
+
+- (BOOL)running
+{
+    return g_main_loop_is_running(priv->loop_ptr);
+}
+
+- (void)quit
+{
+    g_main_loop_quit(priv->loop_ptr);
+}
+
+- (GOCMainContext *)context
+{
+    return priv->ctx;
+}
+
+- (void)free
+{
+    g_main_loop_unref(priv->loop_ptr);
+    [priv->ctx unref];
+    g_slice_free(GOCMainLoopPriv, priv);
+
+    [super free];
+}
+
+ at end
diff --git a/glib-objc/Makefile.am b/glib-objc/Makefile.am
index 2aa3fc6..43d01d7 100644
--- a/glib-objc/Makefile.am
+++ b/glib-objc/Makefile.am
@@ -10,6 +10,7 @@ glibobjcinclude_HEADERS = \
 	GOCHashTable.h \
 	GOCIterable.h \
 	GOCList.h \
+	GOCMain.h \
 	GOCObjectBase.h
 
 libglib_objc_2_0_la_SOURCES = \
@@ -17,10 +18,12 @@ libglib_objc_2_0_la_SOURCES = \
 	$(glibobjcinclude_HEADERS) \
 	GOCHashTable.m \
 	GOCList.m \
+	GOCMain.m \
 	GOCObjectBase.m
 
 libglib_objc_2_0_la_CFLAGS = \
 	-DGLIB_OBJC_COMPILATION \
+	-I$(top_srcdir)/common \
 	$(GLIB_CFLAGS)
 
 libglib_objc_2_0_la_OBJCFLAGS = $(libglib_objc_2_0_la_CFLAGS)
diff --git a/glib-objc/glib-objc.h b/glib-objc/glib-objc.h
index 86a5ddd..b697a00 100644
--- a/glib-objc/glib-objc.h
+++ b/glib-objc/glib-objc.h
@@ -27,6 +27,7 @@
 #import <glib-objc/GOCHashTable.h>
 #import <glib-objc/GOCIterable.h>
 #import <glib-objc/GOCList.h>
+#import <glib-objc/GOCMain.h>
 #import <glib-objc/GOCObjectBase.h>
 
 #undef __IN_GLIB_OBJC_H



More information about the Xfce4-commits mailing list