Add:core:Further works on event system abstraction
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Thu, 4 Sep 2008 16:51:05 +0000 (16:51 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Thu, 4 Sep 2008 16:51:05 +0000 (16:51 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk/navit@1365 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/Makefile.am
navit/event.c
navit/event.h
navit/event_glib.c [new file with mode: 0644]
navit/event_glib.h [new file with mode: 0644]
navit/plugin.h
navit/plugin_def.h
navit/start.c

index 726cdd1..d7cd0cb 100644 (file)
@@ -15,7 +15,7 @@ EXTRA_DIST = navit.xml
 
 noinst_LTLIBRARIES        = libnavit.la
 libnavit_la_SOURCES = attr.c callback.c compass.c coord.c country.c cursor.c data_window.c debug.c \
-       event.c file.c graphics.c gui.c item.c layout.c log.c main.c map.c \
+       event.c event_glib.c file.c graphics.c gui.c item.c layout.c log.c main.c map.c \
        mapset.c maptype.c menu.c navit.c navigation.c osd.c param.c phrase.c plugin.c popup.c \
        profile.c projection.c route.c search.c speech.c transform.c track.c \
        util.c vehicle.c xmlconfig.c attr.h attr_def.h callback.h color.h compass.h coord.h country.h \
index efbb499..456d1c3 100644 (file)
  * Boston, MA  02110-1301, USA.
  */
 
-#include <glib.h>
+#include <string.h>
+#include <stdlib.h>
 #include "event.h"
+#include "plugin.h"
+#include "debug.h"
 
-static GMainLoop *loop;
+static struct event_methods event_methods;
+static char *e_requestor;
+static char *e_system;
 
 void event_main_loop_run(void)
 {
-       loop = g_main_loop_new (NULL, TRUE);
-        if (g_main_loop_is_running (loop))
-        {
-               g_main_loop_run (loop);
+       if (! event_methods.main_loop_run) {
+               dbg(0,"no event system set\n");
+               exit(1);
        }
+       event_methods.main_loop_run();
 }
 
 void event_main_loop_quit(void)
 {
-       if (loop)
-               g_main_loop_quit(loop);
-} 
+       event_methods.main_loop_quit();
+}
 
-void *
+struct event_watch *
 event_add_watch(int fd, int w, struct callback *cb)
 {
+       return event_methods.add_watch(fd, w, cb);
 }
 
 void
-event_remove_watch(void *data)
+event_remove_watch(struct event_watch *ev)
 {
+       event_methods.remove_watch(ev);
 }
 
-void *
-event_add_timeout(int timeout, struct callback *cb)
+struct event_timeout *
+event_add_timeout(int timeout, int multi, struct callback *cb)
 {
+       return event_methods.add_timeout(timeout, multi, cb);
 }
 
 void
-event_remove_timeout(void *data)
+event_remove_timeout(struct event_timeout *ev)
 {
+       event_methods.remove_timeout(ev);
 }
 
-void *
+struct event_idle *
 event_add_idle(struct callback *cb)
 {
+       return event_methods.add_idle(cb);
 }
 
 void
-event_remove_idle(void *data)
+event_remove_idle(struct event_idle *ev)
+{
+       event_methods.remove_idle(ev);
+}
+
+int
+event_request_system(char *system, char *requestor)
 {
+       void (*event_type_new)(struct event_methods *meth);
+       if (e_system) {
+               if (strcmp(e_system, system)) {
+                       dbg(0,"system '%s' already requested by '%s', can't set to '%s' as requested from '%s'\n", e_system, e_requestor, system, requestor);
+                       return 0;
+               }
+               return 1;
+       }
+       event_type_new=plugin_get_event_type(system);
+        if (! event_type_new) {
+               dbg(0,"unsupported event system '%s' requested from '%s'\n", system, requestor);
+                return 0;
+       }
+       event_type_new(&event_methods);
+       e_system=system;
+       e_requestor=requestor;
+       
+       return 1;
 }
+
+
index 7e141e9..897fdff 100644 (file)
  * Boston, MA  02110-1301, USA.
  */
 
+struct event_idle;
+struct event_timeout;
+struct event_watch;
+struct callback;
+
+struct event_methods {
+       void (*main_loop_run)(void);
+       void (*main_loop_quit)(void);
+       struct event_watch *(*add_watch)(int fd, int w, struct callback *cb);
+       void (*remove_watch)(struct event_watch *ev);
+       struct event_timeout *(*add_timeout)(int timeout, int multi, struct callback *cb);
+       void (*remove_timeout)(struct event_timeout *ev);
+       struct event_idle *(*add_idle)(struct callback *cb);
+       void (*remove_idle)(struct event_idle *ev);
+};
+
+/* prototypes */
 void event_main_loop_run(void);
 void event_main_loop_quit(void);
+struct event_watch *event_add_watch(int fd, int w, struct callback *cb);
+void event_remove_watch(struct event_watch *ev);
+struct event_timeout *event_add_timeout(int timeout, int multi, struct callback *cb);
+void event_remove_timeout(struct event_timeout *ev);
+struct event_idle *event_add_idle(struct callback *cb);
+void event_remove_idle(struct event_idle *ev);
+int event_request_system(char *system, char *requestor);
+/* end of prototypes */
diff --git a/navit/event_glib.c b/navit/event_glib.c
new file mode 100644 (file)
index 0000000..69e6358
--- /dev/null
@@ -0,0 +1,143 @@
+/**
+ * Navit, a modular navigation system.
+ * Copyright (C) 2005-2008 Navit Team
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * version 2 as published by the Free Software Foundation.
+ *
+ * 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., 51 Franklin Street, Fifth Floor,
+ * Boston, MA  02110-1301, USA.
+ */
+
+#include <glib.h>
+#include "event.h"
+#include "event_glib.h"
+#include "debug.h"
+#include "callback.h"
+
+static GMainLoop *loop;
+
+static void event_glib_main_loop_run(void)
+{
+       loop = g_main_loop_new (NULL, TRUE);
+        if (g_main_loop_is_running (loop))
+        {
+               g_main_loop_run (loop);
+       }
+}
+
+static void event_glib_main_loop_quit(void)
+{
+       if (loop)
+               g_main_loop_quit(loop);
+}
+
+struct event_watch {
+       GIOChannel *iochan;
+       guint source;
+};
+
+static gboolean
+event_glib_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
+{
+       struct callback *cb=t;
+       callback_call_0(cb);
+       return TRUE;
+}
+
+static struct event_watch *
+event_glib_add_watch(int fd, int w, struct callback *cb)
+{
+       struct event_watch *ret=g_new0(struct event_watch, 1);
+       ret->iochan = g_io_channel_unix_new(fd);
+       ret->source = g_io_add_watch(ret->iochan, G_IO_IN | G_IO_ERR | G_IO_HUP, event_glib_call_watch, (gpointer)cb);
+       return ret;
+}
+
+static void
+event_glib_remove_watch(struct event_watch *ev)
+{
+       GError *error = NULL;
+       g_source_remove(ev->source);
+       g_io_channel_shutdown(ev->iochan, 0, &error);
+       g_free(ev);
+}
+
+struct event_timeout {
+       guint source;
+};
+
+static gboolean
+event_glib_call_timeout_single(gpointer t)
+{
+       struct callback *cb=t;
+       callback_call_0(cb);
+       return FALSE;
+}
+
+static gboolean
+event_glib_call_timeout_multi(gpointer t)
+{
+       struct callback *cb=t;
+       callback_call_0(cb);
+       return TRUE;
+}
+
+
+static struct event_timeout *
+event_glib_add_timeout(int timeout, int multi, struct callback *cb)
+{
+       struct event_timeout *ret=g_new0(struct event_timeout, 1);
+       ret->source = g_timeout_add(timeout, multi ? (GSourceFunc)event_glib_call_timeout_multi : (GSourceFunc)event_glib_call_timeout_single, (gpointer)cb);
+
+       return ret;
+}
+
+static void
+event_glib_remove_timeout(struct event_timeout *ev)
+{
+       g_source_remove(ev->source);
+       g_free(ev);
+}
+
+static struct event_idle *
+event_glib_add_idle(struct callback *cb)
+{
+       return NULL;
+}
+
+static void
+event_glib_remove_idle(struct event_idle *ev)
+{
+}
+
+static struct event_methods event_glib_methods = {
+       event_glib_main_loop_run,
+       event_glib_main_loop_quit,
+       event_glib_add_watch,
+       event_glib_remove_watch,
+       event_glib_add_timeout,
+       event_glib_remove_timeout,
+       event_glib_add_idle,
+       event_glib_remove_idle,
+};
+
+static void
+event_glib_new(struct event_methods *meth)
+{
+       *meth=event_glib_methods;
+}
+
+void
+event_glib_init(void)
+{
+       plugin_register_event_type("glib", event_glib_new);
+}
diff --git a/navit/event_glib.h b/navit/event_glib.h
new file mode 100644 (file)
index 0000000..9f9b2f5
--- /dev/null
@@ -0,0 +1,3 @@
+/* prototypes */
+void event_glib_init(void);
+/* end of prototypes */
index 6668768..5f60a2e 100644 (file)
@@ -32,6 +32,7 @@ enum plugin_type {
        plugin_type_osd,
        plugin_type_speech,
        plugin_type_vehicle,
+       plugin_type_event,
        plugin_type_last,
 };
 #endif
index ce8d899..d948fb1 100644 (file)
@@ -28,3 +28,4 @@ PLUGIN_TYPE(map, (struct map_methods *meth, struct attr **attrs))
 PLUGIN_TYPE(osd, (struct navit *nav, struct osd_methods *meth, struct attr **attrs))
 PLUGIN_TYPE(speech, (char *data, struct speech_methods *meth)) 
 PLUGIN_TYPE(vehicle, (struct vehicle_methods *meth, struct callback_list *cbl, struct attr **attrs)) 
+PLUGIN_TYPE(event, (struct event_methods *meth)) 
index 3f28d6b..5c66e3b 100644 (file)
@@ -30,6 +30,7 @@
 #include "navigation.h"
 #include "debug.h"
 #include "event.h"
+#include "event_glib.h"
 #include "xmlconfig.h"
 #include "file.h"
 #include "search.h"
@@ -71,6 +72,7 @@ int main(int argc, char **argv)
     GList *list = NULL, *li;
 
 
+       event_glib_init();
        main_init(argv[0]);
        main_init_nls();
        debug_init(argv[0]);
@@ -153,6 +155,7 @@ int main(int argc, char **argv)
                printf(_("No instance has been created, exiting\n"));
                exit(1);
        }
+       event_request_system("glib","start");
        event_main_loop_run();
 
        return 0;