Added intial support for POI-Windows and kind of friendsd
authormartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Mon, 16 Jul 2007 18:02:14 +0000 (18:02 +0000)
committermartin-s <martin-s@ffa7fe5e-494d-0410-b361-a75ebd5db220>
Mon, 16 Jul 2007 18:02:14 +0000 (18:02 +0000)
git-svn-id: https://navit.svn.sourceforge.net/svnroot/navit/trunk@350 ffa7fe5e-494d-0410-b361-a75ebd5db220

navit/src/cursor.c
navit/src/navit.c
navit/src/navit.h
navit/src/vehicle.c
navit/src/vehicle.h
navit/src/xmlconfig.c

index 3e226af..2cc36d4 100644 (file)
@@ -1,4 +1,3 @@
-
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -6,6 +5,7 @@
 #include <fcntl.h>
 #include <math.h>
 #include <glib.h>
+#include "debug.h"
 #include "coord.h"
 #include "transform.h"
 #include "projection.h"
@@ -233,7 +233,7 @@ cursor_update(struct vehicle *v, void *data)
 struct cursor *
 cursor_new(struct graphics *gra, struct vehicle *v, struct color *c, struct transformation *t)
 {
-       printf("cursor_new v=%p\n", v);
+       dbg(2,"enter gra=%p v=%p c=%p t=%p\n", gra, v, c, t);
        struct cursor *this=g_new(struct cursor,1);
        this->gra=gra;
        this->trans=t;
@@ -242,6 +242,7 @@ cursor_new(struct graphics *gra, struct vehicle *v, struct color *c, struct tran
        graphics_gc_set_foreground(this->cursor_gc, c);
        graphics_gc_set_linewidth(this->cursor_gc, 2);
        this->vehicle_callback=vehicle_callback_register(v, cursor_update, this);
+       dbg(2,"ret=%p\n", this);
        return this;
 }
 
@@ -260,4 +261,3 @@ cursor_register_update_callback(struct cursor *this, void (*func)(struct cursor
        this->update_callback.data=data;
 }
 
-
index b9f4bf2..a8d134e 100644 (file)
@@ -9,6 +9,7 @@
 #include "navit.h"
 #include "callback.h"
 #include "gui.h"
+#include "item.h"
 #include "map.h"
 #include "mapset.h"
 #include "main.h"
@@ -64,8 +65,7 @@ struct navit {
        struct datawindow *roadbook_window;
 };
 
-struct gui *
-main_loop_gui;
+struct gui *main_loop_gui;
 
 
 void
@@ -458,6 +458,74 @@ navit_window_roadbook_new(struct navit *this_)
        this_->roadbook_window=gui_datawindow_new(this_->gui, "Roadbook", NULL, NULL);
 }
 
+static void
+navit_window_items_new(struct navit *this_)
+{
+       struct datawindow *win;
+       struct map_selection sel;
+       struct coord c,*center;
+       struct mapset_handle *h;
+       struct map *m;
+       struct map_rect *mr;
+       struct item *item;
+       struct attr attr;
+       GHashTable *hash;
+       int idist,dist=100000;
+       enum item_type type1,type2,type3;
+       struct param_list param[4];
+       char distbuf[32];
+
+       param[0].name="Distance";
+       param[1].name="Direction";
+       param[2].name="Type";
+       param[3].name="Name";
+       sel.next=NULL;
+       sel.order[layer_town]=18;
+       sel.order[layer_street]=18;
+       sel.order[layer_poly]=18;
+       center=transform_center(this_->trans);
+       sel.rect.lu.x=center->x-dist;
+       sel.rect.lu.y=center->y+dist;
+       sel.rect.rl.x=center->x+dist;
+       sel.rect.rl.y=center->y-dist;
+       hash=g_hash_table_new(g_int_hash, g_int_equal);
+       type1=type_bookmark;
+       g_hash_table_insert(hash, &type1, 1);
+       type2=type_poi_camping;
+       g_hash_table_insert(hash, &type2, 1);
+       type3=type_roadbook;
+       g_hash_table_insert(hash, &type3, 1);
+       dbg(2,"0x%x,0x%x - 0x%x,0x%x\n", sel.rect.lu.x, sel.rect.lu.y, sel.rect.rl.x, sel.rect.rl.y);
+       win=gui_datawindow_new(this_->gui, "Itemlist", NULL, NULL);
+       h=mapset_open(navit_get_mapset(this_));
+        while ((m=mapset_next(h, 1))) {
+               dbg(2,"m=%p %s\n", m, map_get_filename(m));
+               mr=map_rect_new(m, &sel);
+               dbg(2,"mr=%p\n", mr);
+               while ((item=map_rect_get_item(mr))) {
+                       if (item_coord_get(item, &c, 1)) {
+                               if (coord_rect_contains(&sel.rect, &c) && g_hash_table_lookup(hash, &item->type)) {
+                                       if (! item_attr_get(item, attr_label, &attr)) 
+                                               attr.u.str=NULL;
+                                       idist=transform_distance(center, &c);   
+                                       param[0].value=distbuf; 
+                                       param[1].value="";
+                                       param[2].value=item_to_name(item->type);
+                                       sprintf(distbuf,"%d", idist/1000);
+                                       param[3].value=attr.u.str;
+                                       datawindow_add(win, param, 4);
+                                       /* printf("gefunden %s %s %d\n",item_to_name(item->type), attr.u.str, idist/1000); */
+                               }
+                               if (item->type >= type_line) 
+                                       while (item_coord_get(item, &c, 1));
+                       }
+               }
+               map_rect_destroy(mr);   
+       }
+       mapset_close(h);
+       g_hash_table_destroy(hash);
+}
+
 
 void
 navit_init(struct navit *this_)
@@ -497,6 +565,7 @@ navit_init(struct navit *this_)
        global_navit=this_;
 #if 0
        navit_window_roadbook_new(this_);
+       navit_window_items_new(this_);
 #endif
        navit_debug(this_);
 }
@@ -562,6 +631,7 @@ navit_cursor_update(struct cursor *cursor, void *this__p)
        int dir=cursor_get_dir(cursor);
        int speed=cursor_get_speed(cursor);
 
+
        if (this_->pid && speed > 2)
                kill(this_->pid, SIGWINCH);
 
@@ -607,7 +677,7 @@ navit_set_position(struct navit *this_, struct coord *c)
 }
 
 void
-navit_vehicle_add(struct navit *this_, struct vehicle *v, struct color *c, int update, int follow)
+navit_vehicle_add(struct navit *this_, struct vehicle *v, struct color *c, int update, int follow, int active)
 {
        this_->vehicle=v;
        this_->update_curr=this_->update=update;
index 3e6ed29..da527ff 100644 (file)
@@ -43,7 +43,7 @@ void navit_set_center(struct navit *this_, struct coord *center);
 void navit_set_center_screen(struct navit *this_, struct point *p);
 void navit_toggle_cursor(struct navit *this_);
 void navit_set_position(struct navit *this_, struct coord *c);
-void navit_vehicle_add(struct navit *this_, struct vehicle *v, struct color *c, int update, int follow);
+void navit_vehicle_add(struct navit *this_, struct vehicle *v, struct color *c, int update, int follow, int active);
 void navit_tracking_add(struct navit *this_, struct tracking *tracking);
 void navit_route_add(struct navit *this_, struct route *route);
 void navit_navigation_add(struct navit *this_, struct navigation *navigation);
index 78b5d80..c048f13 100644 (file)
@@ -7,6 +7,10 @@
 #include <fcntl.h>
 #include <math.h>
 #include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
 #include <glib.h>
 #ifdef HAVE_LIBGPS
 #include <gps.h>
@@ -56,7 +60,13 @@ struct vehicle {
 #define BUFFER_SIZE 256
        char buffer[BUFFER_SIZE];
        int buffer_pos;
+       struct vehicle *child;
        GList *callbacks;
+
+       int magic;
+       int is_udp;
+       int interval;
+       struct sockaddr_in rem;
 };
 
 struct vehicle *vehicle_last;
@@ -344,6 +354,114 @@ vehicle_close(struct vehicle *this)
                close(this->fd);
 }
 
+struct packet {
+       int magic __attribute__ ((packed));
+       unsigned char type;
+       union {
+               struct {
+                       int x __attribute__ ((packed));
+                       int y __attribute__ ((packed));
+                       unsigned char speed;
+                       unsigned char dir;
+               } pos;
+       } u;
+} __attribute__ ((packed)) ;
+
+static void
+vehicle_udp_recv(struct vehicle *this)
+{
+       struct packet pkt;
+       int size;
+
+       dbg(2,"enter this=%p",this);
+       size=recv(this->fd, &pkt, sizeof(pkt), 0);
+       if (pkt.magic == this->magic) {
+               dbg(3,"magic 0x%x size=%d\n", pkt.magic, size);
+               this->current_pos.x=pkt.u.pos.x;
+               this->current_pos.y=pkt.u.pos.y;
+               this->speed=pkt.u.pos.speed;
+               this->dir=pkt.u.pos.dir*2;
+               vehicle_call_callbacks(this);
+       }
+}
+
+static void
+vehicle_udp_update(struct vehicle *child, void *data)
+{
+       struct vehicle *this=(struct vehicle *)data;
+       struct coord *pos=&child->current_pos;
+       struct packet pkt;
+       int speed=child->speed;
+       int dir=child->dir/2;
+       if (speed > 255)
+               speed=255;
+       pkt.magic=this->magic;
+       pkt.type=1;
+       pkt.u.pos.x=pos->x;
+       pkt.u.pos.y=pos->y;
+       pkt.u.pos.speed=speed;
+       pkt.u.pos.dir=dir;
+       sendto(this->fd, &pkt, sizeof(pkt), 0, (struct sockaddr *)&this->rem, sizeof(this->rem));
+       this->current_pos=child->current_pos;
+       this->speed=child->speed;
+       this->dir=child->dir;
+       vehicle_call_callbacks(this);
+}
+
+static int
+vehicle_udp_query(void *data)
+{
+       struct vehicle *this=(struct vehicle *)data;
+       struct packet pkt;
+       dbg(2,"enter this=%p\n", this);
+       pkt.magic=this->magic;
+       pkt.type=2;
+       sendto(this->fd, &pkt, 5, 0, (struct sockaddr *)&this->rem, sizeof(this->rem));
+       dbg(2,"ret=TRUE\n");
+       return TRUE;
+}
+
+static int
+vehicle_udp_open(struct vehicle *this)
+{
+       char *host,*child,*url,*colon;
+       int port;
+       url=g_strdup(this->url);
+       colon=index(url+6,':');
+       struct sockaddr_in lcl;
+
+       if (! colon || sscanf(colon+1,"%d/%i/%d", &port, &this->magic, &this->interval) != 3) {
+               g_warning("Wrong syntax in %s\n", this->url);
+               return 0;
+       }
+       host=url+6;
+       *colon='\0';
+       this->fd=socket(PF_INET, SOCK_DGRAM, 0);
+       this->is_udp=1;
+       memset(&lcl, 0, sizeof(lcl));
+       lcl.sin_family = AF_INET;
+
+       this->rem.sin_family = AF_INET;
+       inet_aton(host, &this->rem.sin_addr);
+       this->rem.sin_port=htons(port);
+
+       bind(this->fd, (struct sockaddr *)&lcl, sizeof(lcl));
+       child=index(colon+1,' ');
+       if (child) {
+               child++;
+               if (!this->child) {
+                       dbg(3,"child=%s\n", child);
+                       this->child=vehicle_new(child);
+                       vehicle_callback_register(this->child, vehicle_udp_update, this);
+               }
+       } else {
+               vehicle_udp_query(this);
+               g_timeout_add(this->interval*1000, vehicle_udp_query, this);
+       }
+       g_free(url);
+       return 0;
+}
+
 static int
 vehicle_open(struct vehicle *this)
 {
@@ -404,6 +522,9 @@ vehicle_open(struct vehicle *this)
                g_warning("No support for gpsd compiled in\n");
                return 0;
 #endif
+       } else if (! strncmp(this->url,"udp://",6)) {
+               vehicle_udp_open(this);
+               fd=this->fd;
        }
        this->iochan=g_io_channel_unix_new(fd);
        enable_watch(this);
@@ -428,6 +549,10 @@ vehicle_track(GIOChannel *iochan, GIOCondition condition, gpointer t)
 #else
                {
 #endif
+                       if (this->is_udp) {
+                               vehicle_udp_recv(this);
+                               return TRUE;
+                       }
                        size=read(g_io_channel_unix_get_fd(iochan), this->buffer+this->buffer_pos, BUFFER_SIZE-this->buffer_pos-1);
                        if (size <= 0) {
                                vehicle_close(this);
index 46cf430..cb045a9 100644 (file)
@@ -1,4 +1,5 @@
 /* prototypes */
+enum projection;
 struct coord;
 struct vehicle;
 enum projection vehicle_projection(struct vehicle *this);
@@ -7,6 +8,9 @@ double *vehicle_speed_get(struct vehicle *this);
 double *vehicle_dir_get(struct vehicle *this);
 void vehicle_set_position(struct vehicle *this, struct coord *pos);
 struct vehicle *vehicle_new(const char *url);
+int vehicle_get_active(struct vehicle *this);
+void vehicle_set_active(struct vehicle *this, int active);
 void *vehicle_callback_register(struct vehicle *this, void (*func)(struct vehicle *, void *data), void *data);
 void vehicle_callback_unregister(struct vehicle *this, void *handle);
 void vehicle_destroy(struct vehicle *this);
+/* end of prototypes */
index 577cd1c..944dd86 100644 (file)
@@ -192,7 +192,7 @@ xmlconfig_vehicle(struct xmlstate *state)
        const char *s=find_attribute(state, "source", 1);
        const char *value;
        struct color color;
-       int update=1, follow=0;
+       int update=1, follow=0, active;
 
        if (! s)
                return 0;
@@ -205,8 +205,9 @@ xmlconfig_vehicle(struct xmlstate *state)
                update=convert_number(value);
        if ((value=find_attribute(state, "follow", 0)))
                follow=convert_number(value);
-       
-       navit_vehicle_add(state->parent->element_object, state->element_object, &color, update, follow);
+       active=find_boolean(state, "active", 1, 0);
+
+       navit_vehicle_add(state->parent->element_object, state->element_object, &color, update, follow, active);
        return 1;
 }