Fix:vehicle_file:Converted to event system
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Tue, 11 Nov 2008 18:51:31 +0000 (18:51 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Tue, 11 Nov 2008 18:51:31 +0000 (18:51 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@1721 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/navit/event.c
navit/navit/event.h
navit/navit/event_glib.c
navit/navit/vehicle/file/vehicle_file.c

index 7635d01..f75c3f3 100644 (file)
@@ -43,9 +43,9 @@ void event_main_loop_quit(void)
 }
 
 struct event_watch *
-event_add_watch(struct file *file, enum event_watch_cond cond, struct callback *cb)
+event_add_watch(void *fd, enum event_watch_cond cond, struct callback *cb)
 {
-       return event_methods.add_watch(file, cond, cb);
+       return event_methods.add_watch(fd, cond, cb);
 }
 
 void
index db6675a..c31e8b7 100644 (file)
@@ -37,7 +37,7 @@ enum event_watch_cond {
 struct event_methods {
        void (*main_loop_run)(void);
        void (*main_loop_quit)(void);
-       struct event_watch *(*add_watch)(struct file *file, enum event_watch_cond cond, struct callback *cb);
+       struct event_watch *(*add_watch)(void *fd, enum event_watch_cond cond, 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);
@@ -50,7 +50,7 @@ struct event_methods {
 /* prototypes */
 void event_main_loop_run(void);
 void event_main_loop_quit(void);
-struct event_watch *event_add_watch(struct file *file, enum event_watch_cond cond, struct callback *cb);
+struct event_watch *event_add_watch(void *fd, enum event_watch_cond cond, 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);
index ec7c373..7a4a1eb 100644 (file)
@@ -54,10 +54,10 @@ event_glib_call_watch(GIOChannel * iochan, GIOCondition condition, gpointer t)
 }
 
 static struct event_watch *
-event_glib_add_watch(struct file *file, enum event_watch_cond cond, struct callback *cb)
+event_glib_add_watch(void *fd, enum event_watch_cond cond, struct callback *cb)
 {
        struct event_watch *ret=g_new0(struct event_watch, 1);
-       int flags=0,fd=(int)file_get_os_handle(file);
+       int flags=0;
        ret->iochan = g_io_channel_unix_new(fd);
        switch (cond) {
        case event_watch_cond_read:
@@ -81,7 +81,7 @@ event_glib_remove_watch(struct event_watch *ev)
        if (! ev)
                return;
        g_source_remove(ev->source);
-       g_io_channel_shutdown(ev->iochan, 0, &error);
+       g_io_channel_unref(ev->iochan);
        g_free(ev);
 }
 
index 97fb3d1..c57199c 100644 (file)
@@ -36,6 +36,7 @@
 #include "plugin.h"
 #include "coord.h"
 #include "item.h"
+#include "event.h"
 #include "vehicle.h"
 
 static void vehicle_file_disable_watch(struct vehicle_priv *priv);
@@ -57,8 +58,8 @@ struct vehicle_priv {
        struct callback_list *cbl;
        int fd;
        FILE *file;
-       guint watch;
-       GIOChannel *iochan;
+       struct callback *cb,*cbt;
+       struct event_watch *watch;
        char *buffer;
        int buffer_pos;
        char *nmea_data;
@@ -205,7 +206,6 @@ vehicle_file_open(struct vehicle_priv *priv)
                priv->fd = fileno(priv->file);
                priv->file_type = file_type_pipe;
        }
-       priv->iochan = g_io_channel_unix_new(priv->fd);
 #endif
        return 1;
 }
@@ -214,13 +214,9 @@ static void
 vehicle_file_close(struct vehicle_priv *priv)
 {
 #ifdef _WIN32
-    serial_io_shutdown( priv->fd );
+       serial_io_shutdown( priv->fd );
 #else
-       GError *error = NULL;
-       if (priv->iochan) {
-               g_io_channel_shutdown(priv->iochan, 0, &error);
-               priv->iochan = NULL;
-       }
+       vehicle_file_disable_watch(priv);
        if (priv->file)
                pclose(priv->file);
        else if (priv->fd >= 0)
@@ -231,11 +227,10 @@ vehicle_file_close(struct vehicle_priv *priv)
 }
 
 static int
-vehicle_file_enable_watch_timer(gpointer t)
+vehicle_file_enable_watch_timer(struct vehicle_priv *priv)
 {
-       struct vehicle_priv *priv = t;
-       vehicle_file_enable_watch(priv);
        dbg(1, "enter\n");
+       vehicle_file_enable_watch(priv);
 
        return FALSE;
 }
@@ -337,10 +332,10 @@ vehicle_file_parse(struct vehicle_priv *priv, char *buffer)
 
 #ifndef _WIN32
                if (priv->file_type == file_type_file) {
-                       vehicle_file_disable_watch(priv);
-                       g_timeout_add(priv->time,
-                                     vehicle_file_enable_watch_timer,
-                                     priv);
+                       if (priv->watch) {
+                               vehicle_file_disable_watch(priv);
+                               event_add_timeout(priv->time, 0, priv->cbt);
+                       }
                }
 #endif
        }
@@ -420,62 +415,54 @@ vehicle_file_parse(struct vehicle_priv *priv, char *buffer)
 }
 
 #ifndef _WIN32
-static gboolean
-vehicle_file_io(GIOChannel * iochan, GIOCondition condition, gpointer t)
+static void
+vehicle_file_io(struct vehicle_priv *priv)
 {
-       struct vehicle_priv *priv = t;
        int size, rc = 0;
        char *str, *tok;
 
-       dbg(1, "enter condition=%d\n", condition);
-       if (condition == G_IO_IN) {
-               size =
-                   read(g_io_channel_unix_get_fd(iochan),
-                        priv->buffer + priv->buffer_pos,
-                        buffer_size - priv->buffer_pos - 1);
-               if (size <= 0) {
-                       switch (priv->on_eof) {
-                       case 0:
-                               vehicle_file_close(priv);
-                               vehicle_file_open(priv);
-                               break;
-                       case 1:
-                               return FALSE;
-                               break;
-                       case 2:
-                               exit(0);
-                               break;
-                       }
-                       return TRUE;
-               }
-               priv->buffer_pos += size;
-               priv->buffer[priv->buffer_pos] = '\0';
-               dbg(1, "size=%d pos=%d buffer='%s'\n", size,
-                   priv->buffer_pos, priv->buffer);
-               str = priv->buffer;
-               while ((tok = strchr(str, '\n'))) {
-                       *tok++ = '\0';
-                       dbg(1, "line='%s'\n", str);
-                       rc +=vehicle_file_parse(priv, str);
-                       str = tok;
+       dbg(1, "enter\n");
+       size = read(priv->fd, priv->buffer + priv->buffer_pos, buffer_size - priv->buffer_pos - 1);
+       if (size <= 0) {
+               switch (priv->on_eof) {
+               case 0:
+                       vehicle_file_close(priv);
+                       vehicle_file_open(priv);
+                       break;
+               case 1:
+                       vehicle_file_disable_watch(priv);
+                       break;
+               case 2:
+                       exit(0);
+                       break;
                }
+               return;
+       }
+       priv->buffer_pos += size;
+       priv->buffer[priv->buffer_pos] = '\0';
+       dbg(1, "size=%d pos=%d buffer='%s'\n", size,
+           priv->buffer_pos, priv->buffer);
+       str = priv->buffer;
+       while ((tok = strchr(str, '\n'))) {
+               *tok++ = '\0';
+               dbg(1, "line='%s'\n", str);
+               rc +=vehicle_file_parse(priv, str);
+               str = tok;
+       }
 
-               if (str != priv->buffer) {
-                       size = priv->buffer + priv->buffer_pos - str;
-                       memmove(priv->buffer, str, size + 1);
-                       priv->buffer_pos = size;
-                       dbg(1, "now pos=%d buffer='%s'\n",
-                           priv->buffer_pos, priv->buffer);
-               } else if (priv->buffer_pos == buffer_size - 1) {
-                       dbg(0,
-                           "Overflow. Most likely wrong baud rate or no nmea protocol\n");
-                       priv->buffer_pos = 0;
-               }
-               if (rc)
-                       callback_list_call_0(priv->cbl);
-               return TRUE;
+       if (str != priv->buffer) {
+               size = priv->buffer + priv->buffer_pos - str;
+               memmove(priv->buffer, str, size + 1);
+               priv->buffer_pos = size;
+               dbg(1, "now pos=%d buffer='%s'\n",
+                   priv->buffer_pos, priv->buffer);
+       } else if (priv->buffer_pos == buffer_size - 1) {
+               dbg(0,
+                   "Overflow. Most likely wrong baud rate or no nmea protocol\n");
+               priv->buffer_pos = 0;
        }
-       return FALSE;
+       if (rc)
+               callback_list_call_0(priv->cbl);
 }
 #endif
 
@@ -485,9 +472,8 @@ vehicle_file_enable_watch(struct vehicle_priv *priv)
 #ifdef _WIN32
        g_timeout_add(500, vehicle_win32_serial_track, priv);
 #else
-       priv->watch =
-           g_io_add_watch(priv->iochan, G_IO_IN | G_IO_ERR | G_IO_HUP,
-                          vehicle_file_io, priv);
+       if (! priv->watch)
+               priv->watch = event_add_watch((void *)priv->fd, event_watch_cond_read, priv->cb);
 #endif
 }
 
@@ -496,8 +482,8 @@ vehicle_file_disable_watch(struct vehicle_priv *priv)
 {
 #ifndef _WIN32
        if (priv->watch)
-               g_source_remove(priv->watch);
-       priv->watch = 0;
+               event_remove_watch(priv->watch);
+       priv->watch = NULL;
 #endif
 }
 
@@ -506,6 +492,8 @@ static void
 vehicle_file_destroy(struct vehicle_priv *priv)
 {
        vehicle_file_close(priv);
+       callback_destroy(priv->cb);
+       callback_destroy(priv->cbt);
        if (priv->source)
                g_free(priv->source);
        if (priv->buffer)
@@ -623,13 +611,15 @@ vehicle_file_new_file(struct vehicle_methods
                ret->on_eof=2;
        dbg(0,"on_eof=%d\n", ret->on_eof);
        *meth = vehicle_file_methods;
+       ret->cb=callback_new_1(callback_cast(vehicle_file_io), ret);
+       ret->cbt=callback_new_1(callback_cast(vehicle_file_enable_watch_timer), ret);
        if (vehicle_file_open(ret)) {
                vehicle_file_enable_watch(ret);
                return ret;
        }
 
 #ifdef _WIN32
-    ret->no_data_count = 0;
+       ret->no_data_count = 0;
 #endif
        dbg(0, "Failed to open '%s'\n", ret->source);
        vehicle_file_destroy(ret);