2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2008 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
34 #include "projection.h"
40 #include "transform.h"
46 #include "data_window.h"
48 #include "navigation.h"
56 #include "navit_nls.h"
59 * @defgroup navit the navit core instance. navit is the object containing nearly everything: A set of maps, one or more vehicle, a graphics object for rendering the map, a gui object for displaying the user interface, a route object, a navigation object and so on. Be warned that it is theoretically possible to have more than one navit object
63 //! The navit_vehicule
64 struct navit_vehicle {
66 /*! Limit of the update counter. See navit_add_vehicle */
68 /*! Deprecated : Update counter itself. When it reaches 'update' counts, route is updated */
70 /*! Limit of the follow counter. See navit_add_vehicle */
72 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
78 struct cursor *cursor;
79 struct vehicle *vehicle;
89 struct layout *layout_current;
91 struct action *action;
92 struct transformation *trans;
93 struct compass *compass;
95 struct navigation *navigation;
96 struct speech *speech;
97 struct tracking *tracking;
100 struct displaylist *displaylist;
103 int orient_north_flag;
104 int recentdest_count;
106 GList *windows_items;
107 struct navit_vehicle *vehicle;
108 struct callback_list *attr_cbl;
110 struct callback *nav_speech_cb;
111 struct callback *roadbook_callback;
112 struct callback *popup_callback;
113 struct datawindow *roadbook_window;
114 struct map *bookmark;
115 struct map *former_destination;
116 GHashTable *bookmarks_hash;
117 struct point pressed, last, current;
118 int button_pressed,moved,popped;
119 struct event_timer *button_timeout, *motion_timeout;
120 struct callback *motion_timeout_callback;
122 struct log *textfile_debug_log;
123 struct pcoord destination;
124 int destination_valid;
128 GHashTable *commands;
129 struct callback *resize_callback,*button_callback,*motion_callback;
132 struct gui *main_loop_gui;
137 struct mapset_handle *mapset_handle;
141 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
142 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
143 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
146 navit_add_mapset(struct navit *this_, struct mapset *ms)
148 this_->mapsets = g_list_append(this_->mapsets, ms);
152 navit_get_mapset(struct navit *this_)
155 return this_->mapsets->data;
157 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
163 navit_get_tracking(struct navit *this_)
165 return this_->tracking;
169 navit_draw(struct navit *this_)
172 struct navit_vehicle *nv;
174 if (this_->blocked) {
178 transform_setup_source_rect(this_->trans);
179 graphics_draw(this_->gra, this_->displaylist, this_->mapsets, this_->trans, this_->layout_current);
183 navit_vehicle_draw(this_, nv, NULL);
189 navit_draw_displaylist(struct navit *this_)
191 if (this_->ready == 3)
192 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 1);
196 navit_resize(void *data, int w, int h)
198 struct navit *this_=data;
199 struct map_selection sel;
200 memset(&sel, 0, sizeof(sel));
205 transform_set_screen_selection(this_->trans, &sel);
207 if (this_->ready == 3)
212 navit_get_width(struct navit *this_)
219 navit_get_height(struct navit *this_)
225 navit_popup(void *data)
227 struct navit *this_=data;
228 popup(this_, 1, &this_->pressed);
229 this_->button_timeout=NULL;
235 navit_ignore_button(struct navit *this_)
237 this_->ignore_button=1;
241 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
245 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, pressed, button, p);
246 if (this_->ignore_button) {
247 this_->ignore_button=0;
254 this_->button_pressed=1;
258 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
261 navit_set_center_screen(this_, p);
263 popup(this_, button, p);
265 navit_zoom_in(this_, 2, p);
267 navit_zoom_out(this_, 2, p);
269 this_->button_pressed=0;
270 if (this_->button_timeout) {
271 event_remove_timeout(this_->button_timeout);
272 this_->button_timeout=NULL;
273 if (! this_->moved && ! transform_within_border(this_->trans, p, border))
274 navit_set_center_screen(this_, p);
277 if (this_->motion_timeout) {
278 event_remove_timeout(this_->motion_timeout);
279 this_->motion_timeout=NULL;
284 transform_get_size(this_->trans, &pt.x, &pt.y);
287 pt.x-=this_->last.x-this_->pressed.x;
288 pt.y-=this_->last.y-this_->pressed.y;
289 graphics_draw_drag(this_->gra, NULL);
290 graphics_overlay_disable(this_->gra, 0);
291 navit_set_center_screen(this_, &pt);
299 navit_button(void *data, int pressed, int button, struct point *p)
301 struct navit *this=data;
302 if (! this->popup_callback)
303 this->popup_callback=callback_new_1(navit_popup, this);
304 navit_handle_button(this, pressed, button, p, this->popup_callback);
309 navit_motion_timeout(struct navit *this_)
313 if (this_->drag_bitmap) {
315 point.x=(this_->current.x-this_->pressed.x);
316 point.y=(this_->current.y-this_->pressed.y);
317 if (graphics_draw_drag(this_->gra, &point)) {
318 graphics_overlay_disable(this_->gra, 1);
319 graphics_draw_mode(this_->gra, draw_mode_end);
321 this_->motion_timeout=NULL;
325 dx=(this_->current.x-this_->last.x);
326 dy=(this_->current.y-this_->last.y);
328 this_->last=this_->current;
329 graphics_overlay_disable(this_->gra, 1);
330 graphics_displaylist_move(this_->displaylist, dx, dy);
331 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, 0);
334 this_->motion_timeout=NULL;
339 navit_handle_motion(struct navit *this_, struct point *p)
343 if (this_->button_pressed && !this_->popped) {
344 dx=(p->x-this_->pressed.x);
345 dy=(p->y-this_->pressed.y);
346 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
347 if (this_->button_timeout) {
348 event_remove_timeout(this_->button_timeout);
349 this_->button_timeout=NULL;
352 if (! this_->motion_timeout_callback)
353 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
354 if (! this_->motion_timeout)
355 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
361 navit_motion(void *data, struct point *p)
363 navit_handle_motion((struct navit *)data, p);
367 navit_scale(struct navit *this_, long scale, struct point *p)
369 struct coord c1, c2, *center;
371 transform_reverse(this_->trans, p, &c1);
372 transform_set_scale(this_->trans, scale);
374 transform_reverse(this_->trans, p, &c2);
375 center = transform_center(this_->trans);
376 center->x += c1.x - c2.x;
377 center->y += c1.y - c2.y;
383 * Change the current zoom level, zooming closer to the ground
385 * @param navit The navit instance
386 * @param factor The zoom factor, usually 2
387 * @param p The invariant point (if set to NULL, default to center)
391 navit_zoom_in(struct navit *this_, int factor, struct point *p)
393 long scale=transform_get_scale(this_->trans)/factor;
396 navit_scale(this_, scale, p);
400 * Change the current zoom level
402 * @param navit The navit instance
403 * @param factor The zoom factor, usually 2
404 * @param p The invariant point (if set to NULL, default to center)
408 navit_zoom_out(struct navit *this_, int factor, struct point *p)
410 long scale=transform_get_scale(this_->trans)*factor;
411 navit_scale(this_, scale, p);
415 navit_new(struct attr *parent, struct attr **attrs)
417 struct navit *this_=g_new0(struct navit, 1);
418 struct pcoord center;
421 enum projection pro=projection_mg;
427 main_add_navit(this_);
428 this_->self.type=attr_navit;
429 this_->self.u.navit=this_;
430 this_->attr_cbl=callback_list_new();
432 #if !defined(_WIN32) && !defined(__CEGCC__)
433 f=popen("pidof /usr/bin/ipaq-sleep","r");
435 fscanf(f,"%d",&this_->pid);
436 dbg(1,"ipaq_sleep pid=%d\n", this_->pid);
441 this_->bookmarks_hash=g_hash_table_new_full(g_str_hash, g_str_equal, g_free, NULL);
443 this_->cursor_flag=1;
444 this_->orient_north_flag=0;
445 this_->tracking_flag=1;
446 this_->recentdest_count=10;
448 for (;*attrs; attrs++) {
449 switch((*attrs)->type) {
451 zoom=(*attrs)->u.num;
454 g=*((*attrs)->u.coord_geo);
457 this_->cursor_flag=!!(*attrs)->u.num;
459 case attr_orientation:
460 this_->orient_north_flag=!!(*attrs)->u.num;
463 this_->tracking_flag=!!(*attrs)->u.num;
465 case attr_recent_dest:
466 this_->recentdest_count=(*attrs)->u.num;
468 case attr_drag_bitmap:
469 this_->drag_bitmap=!!(*attrs)->u.num;
472 dbg(0, "Unexpected attribute %x\n",(*attrs)->type);
476 transform_from_geo(pro, &g, &co);
481 this_->trans=transform_new();
482 transform_setup(this_->trans, ¢er, zoom, 0);
483 this_->displaylist=graphics_displaylist_new();
484 this_->commands=g_hash_table_new(g_str_hash, g_str_equal);
485 navit_command_register(this_, "zoom_in", callback_new_3(navit_zoom_in, this_, 2, NULL));
486 navit_command_register(this_, "zoom_out", callback_new_3(navit_zoom_out, this_, 2, NULL));
491 navit_set_gui(struct navit *this_, struct gui *gui)
496 if (gui_has_main_loop(this_->gui)) {
497 if (! main_loop_gui) {
498 main_loop_gui=this_->gui;
500 dbg(0,"gui with main loop already active, ignoring this instance");
508 navit_set_graphics(struct navit *this_, struct graphics *gra)
513 this_->resize_callback=callback_new_attr_1(navit_resize, attr_resize, this_);
514 graphics_add_callback(gra, this_->resize_callback);
515 this_->button_callback=callback_new_attr_1(navit_button, attr_button, this_);
516 graphics_add_callback(gra, this_->button_callback);
517 this_->motion_callback=callback_new_attr_1(navit_motion, attr_motion, this_);
518 graphics_add_callback(gra, this_->motion_callback);
523 navit_get_graphics(struct navit *this_)
529 navit_projection_set(struct navit *this_, enum projection pro)
534 c=transform_center(this_->trans);
535 transform_to_geo(transform_get_projection(this_->trans), c, &g);
536 transform_set_projection(this_->trans, pro);
537 transform_from_geo(pro, &g, c);
542 * @param limit Limits the number of entries in the "backlog". Set to 0 for "infinite"
545 navit_append_coord(struct navit *this_, char *file, struct pcoord *c, char *type, char *description, GHashTable *h, int limit)
561 while ((ch = fgetc(f)) != EOF) {
562 if ((ch == '\n') && (prev != '\n')) {
568 if (prev != '\n') { // Last line did not end with a newline
573 f = fopen(file, "r+");
575 while (lines >= limit) { // We have to "scroll up"
577 numc = 0; // Counts how many bytes we have in our line to scroll up
578 while ((ch = fgetc(f)) != EOF) {
585 buffer=g_malloc(numc);
586 offset = numc; // Offset holds where we currently are
589 fseek(f,offset,SEEK_SET);
590 readc = fread(buffer,1,numc,f);
592 fseek(f,-(numc+readc),SEEK_CUR);
593 fwrite(buffer,1,readc,f);
596 } while (readc == numc);
600 ftruncate(fd,(offset-numc));
614 prostr = projection_to_name(c->pro);
615 fprintf(f,"%s%s%s0x%x %s0x%x type=%s label=\"%s\"\n",
616 prostr, *prostr ? ":" : "",
617 c->x >= 0 ? "":"-", c->x >= 0 ? c->x : -c->x,
618 c->y >= 0 ? "":"-", c->y >= 0 ? c->y : -c->y,
627 parse_line(FILE *f, char *buffer, char **name, struct pcoord *c)
633 enum projection pro = projection_mg;
635 if (! fgets(buffer, 2048, f))
638 pos=coord_parse(cp, pro, &co);
641 if (!cp[pos] || cp[pos] == '\n')
643 cp[strlen(cp)-1]='\0';
645 if (!strncmp(s,"type=", 5)) {
662 * navit_get_user_data_directory
664 * returns the directory used to store user data files (center.txt,
665 * destination.txt, bookmark.txt, ...)
667 * arg: gboolean create: create the directory if it does not exist
670 navit_get_user_data_directory(gboolean create) {
672 dir = getenv("NAVIT_USER_DATADIR");
673 if (create && !file_exists(dir)) {
674 dbg(0,"creating dir %s\n", dir);
675 if (file_mkdir(dir,0)) {
685 * navit_get_destination_file
687 * returns the name of the file used to store destinations with its
690 * arg: gboolean create: create the directory where the file is stored
691 * if it does not exist
694 navit_get_destination_file(gboolean create)
696 return g_strjoin(NULL, navit_get_user_data_directory(create), "destination.txt", NULL);
700 * navit_get_bookmark_file
702 * returns the name of the file used to store bookmarks with its
705 * arg: gboolean create: create the directory where the file is stored
706 * if it does not exist
709 navit_get_bookmark_file(gboolean create)
711 return g_strjoin(NULL, navit_get_user_data_directory(create), "bookmark.txt", NULL);
716 * navit_get_bookmark_file
718 * returns the name of the file used to store the center file with its
721 * arg: gboolean create: create the directory where the file is stored
722 * if it does not exist
725 navit_get_center_file(gboolean create)
727 return g_strjoin(NULL, navit_get_user_data_directory(create), "center.txt", NULL);
731 navit_set_center_from_file(struct navit *this_, char *file)
736 size_t line_size = 0;
738 struct coord *center;
740 f = fopen(file, "r");
743 getline(&line, &line_size, f);
746 center = transform_center(this_->trans);
747 pro = transform_get_projection(this_->trans);
748 coord_parse(g_strchomp(line), pro, center);
755 navit_write_center_to_file(struct navit *this_, char *file)
759 struct coord *center;
761 f = fopen(file, "w+");
763 center = transform_center(this_->trans);
764 pro = transform_get_projection(this_->trans);
765 coord_print(pro, center, f);
775 * Start the route computing to a given set of coordinates
777 * @param navit The navit instance
778 * @param c The coordinate to start routing to
779 * @param description A label which allows the user to later identify this destination in the former destinations selection
783 navit_set_destination(struct navit *this_, struct pcoord *c, char *description)
786 this_->destination=*c;
787 this_->destination_valid=1;
789 this_->destination_valid=0;
790 char *destination_file = navit_get_destination_file(TRUE);
791 navit_append_coord(this_, destination_file, c, "former_destination", description, NULL, this_->recentdest_count);
792 g_free(destination_file);
793 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
795 route_set_destination(this_->route, c);
796 if (this_->navigation) {
797 navigation_flush(this_->navigation);
798 navigation_update(this_->navigation, this_->route);
806 * @brief Checks if a route is calculated
808 * This function checks if a route is calculated.
810 * @param this_ The navit struct whose route should be checked.
811 * @return True if the route is set, false otherwise.
814 navit_check_route(struct navit *this_)
817 return route_get_path_set(this_->route);
824 * Record the given set of coordinates as a bookmark
826 * @param navit The navit instance
827 * @param c The coordinate to store
828 * @param description A label which allows the user to later identify this bookmark
832 navit_add_bookmark(struct navit *this_, struct pcoord *c, const char *description)
834 char *bookmark_file = navit_get_bookmark_file(TRUE);
835 navit_append_coord(this_,bookmark_file, c, "bookmark", description, this_->bookmarks_hash,0);
836 g_free(bookmark_file);
838 callback_list_call_attr_0(this_->attr_cbl, attr_bookmark_map);
841 struct navit *global_navit;
844 navit_add_bookmarks_from_file(struct navit *this_)
846 char *bookmark_file = navit_get_bookmark_file(FALSE);
847 struct attr parent={attr_navit, .u.navit=this_};
848 struct attr type={attr_type, {"textfile"}}, data={attr_data, {bookmark_file}};
849 struct attr *attrs[]={&type, &data, NULL};
851 this_->bookmark=map_new(&parent, attrs);
852 g_free(bookmark_file);
856 navit_former_destinations_active(struct navit *this_)
858 char *destination_file = navit_get_destination_file(FALSE);
862 f=fopen(destination_file,"r");
864 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && buffer[0]!='\n' || buffer[1]!='\n')
868 g_free(destination_file);
873 navit_add_former_destinations_from_file(struct navit *this_)
875 char *destination_file = navit_get_destination_file(FALSE);
876 struct attr parent={attr_navit, .u.navit=this_};
877 struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}};
878 struct attr *attrs[]={&type, &data, NULL};
885 this_->former_destination=map_new(&parent, attrs);
886 g_free(destination_file);
887 if (!this_->route || !navit_former_destinations_active(this_))
889 mr=map_rect_new(this_->former_destination, NULL);
890 while ((item=map_rect_get_item(mr))) {
891 if (item->type == type_former_destination && item_coord_get(item, &c, 1))
894 map_rect_destroy(mr);
895 pc.pro=map_projection(this_->former_destination);
899 route_set_destination(this_->route, &pc);
904 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
909 if (this_->textfile_debug_log && this_->vehicle) {
910 str1=g_strdup_vprintf(fmt, ap);
911 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
912 log_write(this_->textfile_debug_log, str2, strlen(str2));
920 navit_say(struct navit *this_, char *text)
922 speech_say(this_->speech, text);
926 navit_speak(struct navit *this_)
928 struct navigation *nav=this_->navigation;
929 struct map *map=NULL;
930 struct map_rect *mr=NULL;
935 map=navigation_get_map(nav);
937 mr=map_rect_new(map, NULL);
939 item=map_rect_get_item(mr);
940 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
941 speech_say(this_->speech, attr.u.str);
942 navit_textfile_debug_log(this_, "item=point_debug debug=\"speech_say('%s')\"", attr.u.str);
944 map_rect_destroy(mr);
949 navit_window_roadbook_update(struct navit *this_)
951 struct navigation *nav=this_->navigation;
952 struct map *map=NULL;
953 struct map_rect *mr=NULL;
956 struct param_list param[5];
960 datawindow_mode(this_->roadbook_window, 1);
962 map=navigation_get_map(nav);
964 mr=map_rect_new(map, NULL);
965 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
967 dbg(0,"while loop\n");
968 while ((item=map_rect_get_item(mr))) {
969 dbg(0,"item=%p\n", item);
971 item_attr_get(item, attr_navigation_long, &attr);
972 dbg(2, "Command='%s'\n", attr.u.str);
973 param[0].name=_("Command");
974 param[0].value=g_strdup(attr.u.str);
976 item_attr_get(item, attr_length, &attr);
977 dbg(2, "Length=%d\n", attr.u.num);
978 param[1].name=_("Length");
980 if ( attr.u.num >= 2000 )
982 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
986 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
989 item_attr_get(item, attr_time, &attr);
990 dbg(2, "Time=%d\n", attr.u.num);
992 param[2].name=_("Time");
995 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
999 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1002 item_attr_get(item, attr_destination_length, &attr);
1003 dbg(2, "Destlength=%d\n", attr.u.num);
1004 param[3].name=_("Destination Length");
1005 if ( attr.u.num >= 2000 )
1007 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1011 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1014 item_attr_get(item, attr_destination_time, &attr);
1015 dbg(2, "Desttime=%d\n", attr.u.num);
1017 param[4].name=_("Destination Time");
1020 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1024 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1026 datawindow_add(this_->roadbook_window, param, 5);
1028 map_rect_destroy(mr);
1030 datawindow_mode(this_->roadbook_window, 0);
1034 navit_window_roadbook_destroy(struct navit *this_)
1037 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1038 this_->roadbook_window=NULL;
1039 this_->roadbook_callback=NULL;
1042 navit_window_roadbook_new(struct navit *this_)
1044 if (this_->roadbook_callback || this_->roadbook_window) {
1048 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1049 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1050 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1051 navit_window_roadbook_update(this_);
1055 get_direction(char *buffer, int angle, int mode)
1060 sprintf(buffer,"%d",angle);
1063 if (angle < 69 || angle > 291)
1065 if (angle > 111 && angle < 249)
1067 if (angle > 22 && angle < 158)
1069 if (angle > 202 && angle < 338)
1074 angle=(angle+15)/30;
1077 sprintf(buffer,"%d H", angle);
1082 struct navit_window_items {
1083 struct datawindow *win;
1084 struct callback *click;
1092 navit_window_items_click(struct navit *this_, struct navit_window_items *nwi, char **col)
1098 dbg(0,"enter col=%s,%s,%s,%s,%s\n", col[0], col[1], col[2], col[3], col[4]);
1099 sscanf(col[4], "0x%x,0x%x", &c.x, &c.y);
1100 c.pro = projection_mg;
1101 dbg(0,"0x%x,0x%x\n", c.x, c.y);
1102 description=g_strdup_printf("%s %s", nwi->name, col[3]);
1103 navit_set_destination(this_, &c, description);
1104 g_free(description);
1108 navit_window_items_open(struct navit *this_, struct navit_window_items *nwi)
1110 struct map_selection sel;
1111 struct coord c,*center;
1112 struct mapset_handle *h;
1114 struct map_rect *mr;
1118 struct param_list param[5];
1123 dbg(0, "distance=%d\n", nwi->distance);
1124 if (nwi->distance == -1)
1127 dist=nwi->distance*1000;
1128 param[0].name="Distance";
1129 param[1].name="Direction";
1130 param[2].name="Type";
1131 param[3].name="Name";
1135 sel.order[layer_town]=18;
1136 sel.order[layer_street]=18;
1137 sel.order[layer_poly]=18;
1139 sel.order[layer_town]=0;
1140 sel.order[layer_street]=0;
1141 sel.order[layer_poly]=0;
1143 center=transform_center(this_->trans);
1144 sel.u.c_rect.lu.x=center->x-dist;
1145 sel.u.c_rect.lu.y=center->y+dist;
1146 sel.u.c_rect.rl.x=center->x+dist;
1147 sel.u.c_rect.rl.y=center->y-dist;
1148 dbg(2,"0x%x,0x%x - 0x%x,0x%x\n", sel.u.c_rect.lu.x, sel.u.c_rect.lu.y, sel.u.c_rect.rl.x, sel.u.c_rect.rl.y);
1149 nwi->click=callback_new_2(callback_cast(navit_window_items_click), this_, nwi);
1150 nwi->win=gui_datawindow_new(this_->gui, nwi->name, nwi->click, NULL);
1151 h=mapset_open(navit_get_mapset(this_));
1152 while ((m=mapset_next(h, 1))) {
1154 dbg(2,"m=%p %s\n", m, map_get_filename(m));
1156 mr=map_rect_new(m, &sel);
1157 dbg(2,"mr=%p\n", mr);
1158 while ((item=map_rect_get_item(mr))) {
1159 if (item_coord_get(item, &c, 1)) {
1160 if (coord_rect_contains(&sel.u.c_rect, &c) && g_hash_table_lookup(nwi->hash, &item->type)) {
1161 if (! item_attr_get(item, attr_label, &attr))
1163 idist=transform_distance(map_projection(item->map), center, &c);
1165 get_direction(dirbuf, transform_get_angle_delta(center, &c, 0), 1);
1166 param[0].value=distbuf;
1167 param[1].value=dirbuf;
1168 param[2].value=item_to_name(item->type);
1169 sprintf(distbuf,"%d", idist/1000);
1170 param[3].value=attr.u.str;
1171 sprintf(coordbuf, "0x%x,0x%x", c.x, c.y);
1172 param[4].value=coordbuf;
1173 datawindow_add(nwi->win, param, 5);
1175 /* printf("gefunden %s %s %d\n",item_to_name(item->type), attr.u.str, idist/1000); */
1177 if (item->type >= type_line)
1178 while (item_coord_get(item, &c, 1));
1181 map_rect_destroy(mr);
1186 struct navit_window_items *
1187 navit_window_items_new(const char *name, int distance)
1189 struct navit_window_items *nwi=g_new0(struct navit_window_items, 1);
1190 nwi->name=g_strdup(name);
1191 nwi->distance=distance;
1192 nwi->hash=g_hash_table_new(g_int_hash, g_int_equal);
1198 navit_window_items_add_item(struct navit_window_items *nwi, enum item_type type)
1200 nwi->list=g_list_prepend(nwi->list, (void *)type);
1201 g_hash_table_insert(nwi->hash, &nwi->list->data, (void *)1);
1205 navit_add_window_items(struct navit *this_, struct navit_window_items *nwi)
1207 this_->windows_items=g_list_append(this_->windows_items, nwi);
1211 navit_add_menu_windows_items(struct navit *this_, struct menu *men)
1213 struct navit_window_items *nwi;
1214 struct callback *cb;
1216 l=this_->windows_items;
1219 cb=callback_new_2(callback_cast(navit_window_items_open), this_, nwi);
1220 menu_add(men, nwi->name, menu_type_menu, cb);
1226 navit_init(struct navit *this_)
1231 struct navit_vehicle *nv;
1235 navit_destroy(this_);
1239 dbg(0,"no graphics\n");
1240 navit_destroy(this_);
1243 if (gui_set_graphics(this_->gui, this_->gra)) {
1244 struct attr attr_type_gui, attr_type_graphics;
1245 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1246 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1247 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1248 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1249 dbg(0," for explanations and solutions\n");
1251 navit_destroy(this_);
1254 graphics_init(this_->gra);
1257 dbg(1,"parsed one vehicle\n");
1259 nv->cursor=cursor_new(this_->gra, &nv->c, nv->c2, nv->animate_cursor);
1260 nv->callback.type=attr_callback;
1261 nv->callback.u.callback=callback_new_2(callback_cast(navit_vehicle_update), this_, nv);
1262 vehicle_add_attr(nv->vehicle, &nv->callback);
1263 vehicle_set_attr(nv->vehicle, &this_->self, NULL);
1266 if (this_->mapsets) {
1267 ms=this_->mapsets->data;
1269 if ((map=route_get_map(this_->route)))
1270 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1271 if ((map=route_get_graph_map(this_->route))) {
1272 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1273 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1275 route_set_mapset(this_->route, ms);
1276 route_set_projection(this_->route, transform_get_projection(this_->trans));
1278 if (this_->tracking) {
1279 tracking_set_mapset(this_->tracking, ms);
1281 tracking_set_route(this_->tracking, this_->route);
1283 if (this_->navigation) {
1284 if ((map=navigation_get_map(this_->navigation))) {
1285 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1286 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1289 if (this_->tracking) {
1290 if ((map=tracking_get_map(this_->tracking))) {
1291 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1292 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1295 navit_add_bookmarks_from_file(this_);
1296 navit_add_former_destinations_from_file(this_);
1298 if (this_->navigation && this_->speech) {
1299 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1300 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1302 char *center_file = navit_get_center_file(FALSE);
1303 navit_set_center_from_file(this_, center_file);
1304 g_free(center_file);
1306 if (this_->menubar) {
1307 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1309 navit_add_menu_windows_items(this_, men);
1315 navit_window_roadbook_new(this_);
1316 navit_window_items_new(this_);
1318 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1320 if (this_->ready == 3)
1325 * Change the current zoom level
1327 * @param navit The navit instance
1328 * @param center The point where to center the map, including its projection
1332 navit_set_center(struct navit *this_, struct pcoord *center)
1334 struct coord *c=transform_center(this_->trans);
1336 enum projection pro = transform_get_projection(this_->trans);
1337 if (pro != center->pro) {
1340 transform_from_to(&c1, center->pro, &c2, pro);
1346 if (this_->ready == 3)
1351 navit_set_center_cursor(struct navit *this_, struct coord *cursor, int dir, int xpercent, int ypercent)
1353 struct coord *c=transform_center(this_->trans);
1358 transform_get_size(this_->trans, &width, &height);
1360 transform_set_angle(this_->trans, dir);
1361 p.x=(100-xpercent)*width/100;
1362 p.y=(100-ypercent)*height/100;
1363 transform_reverse(this_->trans, &p, &cnew);
1365 if (this_->ready == 3)
1371 navit_set_center_screen(struct navit *this_, struct point *p)
1375 transform_reverse(this_->trans, p, &c);
1378 pc.pro = transform_get_projection(this_->trans);
1379 navit_set_center(this_, &pc);
1383 navit_set_attr(struct navit *this_, struct attr *attr)
1385 int dir=0, orient_old=0, attr_updated=0;
1387 switch (attr->type) {
1389 if (this_->cursor_flag != !!attr->u.num) {
1390 this_->cursor_flag=!!attr->u.num;
1395 if(this_->layout_current!=attr->u.layout) {
1396 this_->layout_current=attr->u.layout;
1397 graphics_font_destroy_all(this_->gra);
1402 case attr_orientation:
1403 orient_old=this_->orient_north_flag;
1404 this_->orient_north_flag=!!attr->u.num;
1405 if (this_->orient_north_flag) {
1408 if (this_->vehicle) {
1409 dir = this_->vehicle->dir;
1412 transform_set_angle(this_->trans, dir);
1413 if (orient_old != this_->orient_north_flag) {
1418 case attr_projection:
1419 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1420 navit_projection_set(this_, attr->u.projection);
1425 if (this_->tracking_flag != !!attr->u.num) {
1426 this_->tracking_flag=!!attr->u.num;
1431 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1435 if (((struct navit_vehicle *)l->data)->vehicle == attr->u.vehicle) {
1436 this_->vehicle=(struct navit_vehicle *)l->data;
1447 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1453 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1456 case attr_bookmark_map:
1457 attr->u.map=this_->bookmark;
1460 attr->u.num=this_->cursor_flag;
1462 case attr_destination:
1463 if (! this_->destination_valid)
1465 attr->u.pcoord=&this_->destination;
1467 case attr_former_destination_map:
1468 attr->u.map=this_->former_destination;
1473 iter->u.list=g_list_next(iter->u.list);
1475 iter->u.list=this_->layouts;
1479 attr->u.layout=(struct layout *)iter->u.list->data;
1481 attr->u.layout=this_->layout_current;
1485 if (iter && this_->mapsets) {
1486 if (!iter->u.mapset_handle) {
1487 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
1489 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
1491 mapset_close(iter->u.mapset_handle);
1498 case attr_orientation:
1499 attr->u.num=this_->orient_north_flag;
1501 case attr_projection:
1503 attr->u.num=transform_get_projection(this_->trans);
1509 attr->u.num=this_->tracking_flag;
1514 iter->u.list=g_list_next(iter->u.list);
1516 iter->u.list=this_->vehicles;
1520 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
1522 if(this_->vehicle) {
1523 attr->u.vehicle=this_->vehicle->vehicle;
1537 navit_add_log(struct navit *this_, struct log *log)
1539 struct attr type_attr;
1540 if (!log_get_attr(log, attr_type, &type_attr, NULL))
1542 if (!strcmp(type_attr.u.str, "textfile_debug")) {
1543 if (this_->textfile_debug_log)
1545 this_->textfile_debug_log=log;
1552 navit_add_attr(struct navit *this_, struct attr *attr)
1554 switch (attr->type) {
1556 return navit_add_log(this_, attr->u.log);
1558 return navit_set_gui(this_, attr->u.gui);
1560 return navit_set_graphics(this_, attr->u.graphics);
1562 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
1563 if(!this_->layout_current)
1564 this_->layout_current=attr->u.layout;
1567 this_->route=attr->u.route;
1570 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
1572 case attr_navigation:
1573 this_->navigation=attr->u.navigation;
1575 case attr_recent_dest:
1576 this_->recentdest_count = attr->u.num;
1579 this_->speech=attr->u.speech;
1582 this_->tracking=attr->u.tracking;
1585 return navit_add_vehicle(this_, attr->u.vehicle);
1593 navit_attr_iter_new()
1595 return g_new0(struct attr_iter, 1);
1599 navit_attr_iter_destroy(struct attr_iter *iter)
1605 navit_add_callback(struct navit *this_, struct callback *cb)
1607 callback_list_add(this_->attr_cbl, cb);
1611 navit_remove_callback(struct navit *this_, struct callback *cb)
1613 callback_list_remove(this_->attr_cbl, cb);
1617 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
1619 * @param navit The navit instance
1624 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
1627 enum projection pro;
1633 pro=transform_get_projection(this_->trans);
1634 transform(this_->trans, pro, &nv->coord, &pnt2, 1, 0);
1637 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
1639 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
1644 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
1646 struct attr attr_dir, attr_speed, attr_pos;
1647 struct pcoord cursor_pc;
1648 struct point cursor_pnt, *pnt=&cursor_pnt;
1649 enum projection pro;
1651 int route_path_set=0;
1653 if (this_->ready != 3)
1656 if (! vehicle_get_attr(nv->vehicle, attr_position_direction, &attr_dir, NULL) ||
1657 ! vehicle_get_attr(nv->vehicle, attr_position_speed, &attr_speed, NULL) ||
1658 ! vehicle_get_attr(nv->vehicle, attr_position_coord_geo, &attr_pos, NULL))
1660 nv->dir=*attr_dir.u.numd;
1661 nv->speed=*attr_speed.u.numd;
1662 pro=transform_get_projection(this_->trans);
1663 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
1664 if (nv != this_->vehicle) {
1665 navit_vehicle_draw(this_, nv, NULL);
1669 route_path_set=route_get_path_set(this_->route);
1670 if (this_->tracking && this_->tracking_flag) {
1671 if (tracking_update(this_->tracking, &nv->coord, nv->dir)) {
1672 if (this_->route && nv->update_curr == 1) {
1673 route_set_position_from_tracking(this_->route, this_->tracking);
1674 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1678 if (this_->route && nv->update_curr == 1) {
1679 cursor_pc.pro = pro;
1680 cursor_pc.x = nv->coord.x;
1681 cursor_pc.y = nv->coord.y;
1682 navit_set_position(this_, &cursor_pc);
1685 transform(this_->trans, pro, &nv->coord, &cursor_pnt, 1, 0);
1686 if (!transform_within_border(this_->trans, &cursor_pnt, border)) {
1687 if (!this_->cursor_flag)
1689 if (nv->follow_curr != 1) {
1690 if (this_->orient_north_flag)
1691 navit_set_center_cursor(this_, &nv->coord, 0, 50 - 30.*sin(M_PI*nv->dir/180.), 50 + 30.*cos(M_PI*nv->dir/180.));
1693 navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1699 if (this_->pid && nv->speed > 2)
1700 kill(this_->pid, SIGWINCH);
1702 if (this_->route && nv->update_curr == 1)
1703 navigation_update(this_->navigation, this_->route);
1704 if (this_->cursor_flag && nv->follow_curr == 1) {
1705 navit_set_center_cursor(this_, &nv->coord, nv->dir, 50, 80);
1708 if (pnt && this_->route && !route_path_set && route_get_path_set(this_->route))
1710 if (nv->follow_curr > 1)
1713 nv->follow_curr=nv->follow;
1714 if (nv->update_curr > 1)
1717 nv->update_curr=nv->update;
1718 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
1720 navit_vehicle_draw(this_, nv, pnt);
1722 /* Finally, if we reached our destination, stop navigation. */
1723 if (route_destination_reached(this_->route)) {
1724 navit_set_destination(this_, NULL, NULL);
1729 * Set the position of the vehicle
1731 * @param navit The navit instance
1732 * @param c The coordinate to set as position
1737 navit_set_position(struct navit *this_, struct pcoord *c)
1740 route_set_position(this_->route, c);
1741 callback_list_call_attr_0(this_->attr_cbl, attr_position);
1742 if (this_->navigation) {
1743 navigation_update(this_->navigation, this_->route);
1750 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
1756 * Register a new vehicle
1758 * @param navit The navit instance
1759 * @param v The vehicle instance
1760 * @returns 1 for success
1763 navit_add_vehicle(struct navit *this_, struct vehicle *v)
1765 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
1766 struct attr update,follow,color,active, color2, animate;
1770 nv->animate_cursor=0;
1771 if ((vehicle_get_attr(v, attr_update, &update, NULL)))
1772 nv->update=nv->update=update.u.num;
1773 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
1774 nv->follow=nv->follow=follow.u.num;
1775 if ((vehicle_get_attr(v, attr_color, &color, NULL)))
1776 nv->c=*(color.u.color);
1777 if ((vehicle_get_attr(v, attr_color2, &color2, NULL)))
1778 nv->c2=color2.u.color;
1781 nv->update_curr=nv->update;
1782 nv->follow_curr=nv->follow;
1783 this_->vehicles=g_list_append(this_->vehicles, nv);
1784 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
1785 navit_set_vehicle(this_, nv);
1786 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
1787 nv->animate_cursor=animate.u.num;
1795 navit_get_gui(struct navit *this_)
1800 struct transformation *
1801 navit_get_trans(struct navit *this_)
1803 return this_->trans;
1807 navit_get_route(struct navit *this_)
1809 return this_->route;
1813 navit_get_navigation(struct navit *this_)
1815 return this_->navigation;
1818 struct displaylist *
1819 navit_get_displaylist(struct navit *this_)
1821 return this_->displaylist;
1825 navit_block(struct navit *this_, int block)
1828 this_->blocked |= 1;
1831 if (this_->blocked & 2) {
1841 navit_command_register(struct navit *this_, char *command, struct callback *cb)
1843 dbg(1,"registering '%s'\n", command);
1844 g_hash_table_insert(this_->commands, command, cb);
1850 navit_command_unregister(struct navit *this_, char *command)
1852 struct callback *ret=g_hash_table_lookup(this_->commands, command);
1854 g_hash_table_remove(this_->commands, command);
1861 navit_command_call(struct navit *this_, char *command)
1863 struct callback *cb=g_hash_table_lookup(this_->commands, command);
1864 dbg(0,"calling callback %p for '%s'\n", cb, command);
1867 callback_call_1(cb, command);
1871 navit_destroy(struct navit *this_)
1873 /* TODO: destroy objects contained in this_ */
1874 main_remove_navit(this_);
1875 char *center_file = navit_get_center_file(TRUE);
1876 navit_write_center_to_file(this_, center_file);
1877 g_free(center_file);
1878 callback_destroy(navit_command_unregister(this_, "zoom_in"));
1879 callback_destroy(navit_command_unregister(this_, "zoom_out"));
1880 g_hash_table_destroy(this_->commands);