[gnode] Add mainloop integration



commit 679b7da1c69a1cab310bef6515a4dd9b8d96a28c
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Fri Nov 27 14:57:10 2015 -0800

    Add mainloop integration

 binding.gyp  |    1 +
 lib/gnode.js |    4 +++
 src/gi.cc    |   10 +++++++
 src/loop.cc  |   79 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 src/loop.h   |   29 +++++++++++++++++++++
 test.js      |    2 +
 6 files changed, 125 insertions(+), 0 deletions(-)
---
diff --git a/binding.gyp b/binding.gyp
index cd8253a..b1730df 100644
--- a/binding.gyp
+++ b/binding.gyp
@@ -3,6 +3,7 @@
         {
             "target_name": "gi",
             "sources": [
+                "src/loop.cc",
                 "src/gi.cc",
                 "src/value.cc",
                 "src/function.cc",
diff --git a/lib/gnode.js b/lib/gnode.js
index 22f83a9..d0c16cd 100644
--- a/lib/gnode.js
+++ b/lib/gnode.js
@@ -15,3 +15,7 @@ exports.importNS = function(name, version) {
 
     return ns;
 };
+
+exports.startLoop = function() {
+    _gi.startLoop();
+};
diff --git a/src/gi.cc b/src/gi.cc
index c81c5fb..748cf40 100644
--- a/src/gi.cc
+++ b/src/gi.cc
@@ -26,6 +26,7 @@
 #include "value.h"
 #include "function.h"
 #include "gobject.h"
+#include "loop.h"
 
 using namespace v8;
 
@@ -128,8 +129,17 @@ static Handle<Value> ImportNS(const Arguments& args) {
     return scope.Close (module_obj);
 }
 
+static Handle<Value> StartLoop(const Arguments& args) {
+    HandleScope scope;
+
+    GNodeJS::StartLoop ();
+
+    return scope.Close (Undefined ());
+}
+
 void InitModule(Handle<Object> exports) {
     exports->Set(String::NewSymbol("importNS"), FunctionTemplate::New(ImportNS)->GetFunction());
+    exports->Set(String::NewSymbol("startLoop"), FunctionTemplate::New(StartLoop)->GetFunction());
 }
 
 NODE_MODULE(gi, InitModule)
diff --git a/src/loop.cc b/src/loop.cc
new file mode 100644
index 0000000..9f9eda3
--- /dev/null
+++ b/src/loop.cc
@@ -0,0 +1,79 @@
+/*
+ * Copyright (C) 2015 Endless Mobile
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written by:
+ *     Jasper St. Pierre <jstpierre mecheye net>
+ */
+
+#include <glib.h>
+#include <uv.h>
+
+/* Integration for the GLib main loop and UV's main loop */
+
+namespace GNodeJS {
+
+struct uv_loop_source {
+    GSource source;
+    uv_loop_t *loop;
+};
+
+static gboolean uv_loop_source_prepare (GSource *base, int *timeout) {
+    struct uv_loop_source *source = (struct uv_loop_source *) base;
+    uv_update_time (source->loop);
+
+    int t = uv_backend_timeout (source->loop);
+
+    if (t == 0) {
+        return TRUE;
+    } else {
+        *timeout = t;
+        return FALSE;
+    }
+}
+
+static gboolean uv_loop_source_dispatch (GSource *base, GSourceFunc callback, gpointer user_data) {
+    struct uv_loop_source *source = (struct uv_loop_source *) base;
+    uv_run (source->loop, UV_RUN_NOWAIT);
+    return G_SOURCE_CONTINUE;
+}
+
+static GSourceFuncs uv_loop_source_funcs = {
+    uv_loop_source_prepare,
+    NULL,
+    uv_loop_source_dispatch,
+    NULL,
+
+    NULL, NULL,
+};
+
+static GSource *uv_loop_source_new (uv_loop_t *loop)
+{
+    struct uv_loop_source *source = (struct uv_loop_source *) g_source_new (&uv_loop_source_funcs, sizeof 
(*source));
+    source->loop = loop;
+    g_source_add_unix_fd (&source->source,
+                          uv_backend_fd (loop),
+                          (GIOCondition) (G_IO_IN | G_IO_ERR));
+    return &source->source;
+}
+
+void StartLoop() {
+    GSource *source = uv_loop_source_new (uv_default_loop ());
+    g_source_attach (source, NULL);
+}
+
+};
diff --git a/src/loop.h b/src/loop.h
new file mode 100644
index 0000000..17df640
--- /dev/null
+++ b/src/loop.h
@@ -0,0 +1,29 @@
+/*
+ * Copyright (C) 2015 Endless Mobile
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License as
+ * published by the Free Software Foundation; either version 2 of the
+ * License, or (at your option) any later version.
+ *
+ * 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
+ * 02111-1307, USA.
+ *
+ * Written by:
+ *     Jasper St. Pierre <jstpierre mecheye net>
+ */
+
+#pragma once
+
+namespace GNodeJS {
+
+void StartLoop();
+
+};
diff --git a/test.js b/test.js
index 67d00db..3c6d75b 100644
--- a/test.js
+++ b/test.js
@@ -1,5 +1,6 @@
 
 const GNode = require('gnode');
+GNode.startLoop();
 
 const GLib = GNode.importNS("GLib");
 console.log(GLib.ascii_strup("foo", -1));
@@ -17,4 +18,5 @@ Gtk.init(0, null);
 var w = new Gtk.Window();
 w.show_all();
 
+setTimeout(function() { console.log("AA"); }, 2000);
 Gtk.main();


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]