2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2009 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.
20 #define _USE_MATH_DEFINES 1
38 #include "projection.h"
44 #include "transform.h"
49 #include "data_window.h"
51 #include "navigation.h"
62 #include "navit_nls.h"
66 #include "vehicleprofile.h"
68 #include "bookmarks.h"
71 * @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
75 //! The navit_vehicule
76 struct navit_vehicle {
78 /*! Limit of the follow counter. See navit_add_vehicle */
80 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
84 struct coord last; /*< Position of the last update of this vehicle */
85 struct vehicle *vehicle;
95 struct layout *layout_current;
97 struct action *action;
98 struct transformation *trans, *trans_cursor;
99 struct compass *compass;
101 struct navigation *navigation;
102 struct speech *speech;
103 struct tracking *tracking;
106 struct displaylist *displaylist;
109 int recentdest_count;
110 int osd_configuration;
112 GList *windows_items;
113 struct navit_vehicle *vehicle;
114 struct callback_list *attr_cbl;
115 struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb, *progress_cb;
116 struct datawindow *roadbook_window;
117 struct map *former_destination;
118 struct point pressed, last, current;
119 int button_pressed,moved,popped,zoomed;
124 struct event_timeout *button_timeout, *motion_timeout;
125 struct callback *motion_timeout_callback;
127 int ignore_graphics_events;
128 struct log *textfile_debug_log;
129 struct pcoord destination;
130 int destination_valid;
135 struct messagelist *messages;
136 struct callback *resize_callback,*button_callback,*motion_callback,*predraw_callback;
137 struct vehicleprofile *vehicleprofile;
138 GList *vehicleprofiles;
143 int zoom_min, zoom_max;
145 struct bookmarks *bookmarks;
147 /* 1=No graphics ok */
153 struct gui *main_loop_gui;
158 struct mapset_handle *mapset_handle;
162 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
163 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
164 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
165 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
166 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
167 static void navit_set_cursors(struct navit *this_);
168 static void navit_cmd_zoom_to_route(struct navit *this);
169 static void navit_cmd_set_center_cursor(struct navit *this_);
170 static void navit_cmd_announcer_toggle(struct navit *this_);
171 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
173 struct navit *global_navit;
176 navit_add_mapset(struct navit *this_, struct mapset *ms)
178 this_->mapsets = g_list_append(this_->mapsets, ms);
182 navit_get_mapset(struct navit *this_)
185 return this_->mapsets->data;
187 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
193 navit_get_tracking(struct navit *this_)
195 return this_->tracking;
199 * @brief Get the user data directory.
200 * @param[in] create - create the directory if it does not exist
202 * @return char * to the data directory string.
204 * returns the directory used to store user data files (center.txt,
205 * destination.txt, bookmark.txt, ...)
209 navit_get_user_data_directory(int create) {
211 dir = getenv("NAVIT_USER_DATADIR");
212 if (create && !file_exists(dir)) {
213 dbg(0,"creating dir %s\n", dir);
214 if (file_mkdir(dir,0)) {
215 dbg(0,"failed creating dir %s\n", dir);
220 } /* end: navit_get_user_data_directory(gboolean create) */
224 navit_draw_async(struct navit *this_, int async)
227 if (this_->blocked) {
231 transform_setup_source_rect(this_->trans);
232 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
236 navit_draw(struct navit *this_)
238 if (this_->ready == 3)
239 navit_draw_async(this_, 0);
243 navit_get_ready(struct navit *this_)
251 navit_draw_displaylist(struct navit *this_)
253 if (this_->ready == 3)
254 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
258 navit_map_progress(struct navit *this_)
262 struct mapset_handle *msh;
265 if (this_->ready != 3)
270 ms=this_->mapsets->data;
272 while (msh && (map=mapset_next(msh, 0))) {
273 if (map_get_attr(map, attr_progress, &attr, NULL)) {
274 char *str=g_strdup_printf("%s ",attr.u.str);
275 graphics_draw_mode(this_->gra, draw_mode_begin);
276 graphics_draw_text_std(this_->gra, 16, str, &p);
279 graphics_draw_mode(this_->gra, draw_mode_end);
286 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
289 if (attr->type != attr_route_status)
292 if (this_->ready != 3)
294 if (updated != route_status_path_done_new)
296 if (this_->vehicle) {
297 if (this_->vehicle->follow_curr == 1)
299 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
300 this_->vehicle->follow_curr=this_->vehicle->follow;
306 navit_handle_resize(struct navit *this_, int w, int h)
308 struct map_selection sel;
309 int callback=(this_->ready == 1);
311 memset(&sel, 0, sizeof(sel));
316 transform_set_screen_selection(this_->trans, &sel);
317 graphics_init(this_->gra);
318 graphics_set_rect(this_->gra, &sel.u.p_rect);
320 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
321 if (this_->ready == 3)
322 navit_draw_async(this_, 1);
326 navit_resize(void *data, int w, int h)
328 struct navit *this=data;
329 if (!this->ignore_graphics_events)
330 navit_handle_resize(this, w, h);
334 navit_get_width(struct navit *this_)
341 navit_get_height(struct navit *this_)
347 navit_popup(void *data)
349 struct navit *this_=data;
350 popup(this_, 1, &this_->pressed);
351 this_->button_timeout=NULL;
357 navit_ignore_button(struct navit *this_)
359 if (this_->ignore_button)
361 this_->ignore_button=1;
366 navit_ignore_graphics_events(struct navit *this_, int ignore)
368 this_->ignore_graphics_events=ignore;
372 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
377 double angleo,anglen;
379 if (!transform_reverse(tr, old, &co))
382 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
383 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
384 yaw=transform_get_yaw(tr)+angleo-anglen;
385 transform_set_yaw(tr, yaw % 360);
387 if (!transform_reverse(tr, new, &cn))
389 cp=transform_get_center(tr);
392 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
393 transform_set_center(tr, &c);
397 navit_set_timeout(struct navit *this_)
400 follow.type=attr_follow;
401 follow.u.num=this_->center_timeout;
402 navit_set_attr(this_, &follow);
406 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
410 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
411 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
412 if (this_->ignore_button) {
413 this_->ignore_button=0;
421 this_->button_pressed=1;
425 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
428 navit_set_center_screen(this_, p, 1);
430 popup(this_, button, p);
431 if (button == 4 && this_->use_mousewheel) {
433 navit_zoom_in(this_, 2, p);
435 if (button == 5 && this_->use_mousewheel) {
437 navit_zoom_out(this_, 2, p);
441 this_->button_pressed=0;
442 if (this_->button_timeout) {
443 event_remove_timeout(this_->button_timeout);
444 this_->button_timeout=NULL;
445 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
446 navit_set_center_screen(this_, p, !this_->zoomed);
449 if (this_->motion_timeout) {
450 event_remove_timeout(this_->motion_timeout);
451 this_->motion_timeout=NULL;
458 update_transformation(this_->trans, &this_->pressed, p, &pr);
460 update_transformation(this_->trans, &this_->pressed, p, NULL);
462 graphics_draw_drag(this_->gra, NULL);
463 transform_copy(this_->trans, this_->trans_cursor);
464 graphics_overlay_disable(this_->gra, 0);
466 navit_set_timeout(this_);
475 navit_button(void *data, int pressed, int button, struct point *p)
477 struct navit *this=data;
478 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
479 if (!this->ignore_graphics_events) {
480 if (! this->popup_callback)
481 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
482 navit_handle_button(this, pressed, button, p, this->popup_callback);
488 navit_motion_timeout(struct navit *this_)
492 if (this_->drag_bitmap) {
494 point.x=(this_->current.x-this_->pressed.x);
495 point.y=(this_->current.y-this_->pressed.y);
496 if (graphics_draw_drag(this_->gra, &point)) {
497 graphics_overlay_disable(this_->gra, 1);
498 graphics_draw_mode(this_->gra, draw_mode_end);
500 this_->motion_timeout=NULL;
504 dx=(this_->current.x-this_->last.x);
505 dy=(this_->current.y-this_->last.y);
507 struct transformation *tr;
509 this_->last=this_->current;
510 graphics_overlay_disable(this_->gra, 1);
511 tr=transform_dup(this_->trans);
515 update_transformation(tr, &this_->pressed, &this_->current, &pr);
517 update_transformation(tr, &this_->pressed, &this_->current, NULL);
520 graphics_displaylist_move(this_->displaylist, dx, dy);
522 graphics_draw_cancel(this_->gra, this_->displaylist);
523 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
524 transform_destroy(tr);
527 this_->motion_timeout=NULL;
532 navit_handle_motion(struct navit *this_, struct point *p)
536 if (this_->button_pressed && !this_->popped) {
537 dx=(p->x-this_->pressed.x);
538 dy=(p->y-this_->pressed.y);
539 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
541 if (this_->button_timeout) {
542 event_remove_timeout(this_->button_timeout);
543 this_->button_timeout=NULL;
546 if (! this_->motion_timeout_callback)
547 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
548 if (! this_->motion_timeout)
549 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
555 navit_motion(void *data, struct point *p)
557 struct navit *this=data;
558 if (!this->ignore_graphics_events)
559 navit_handle_motion(this, p);
563 navit_predraw(struct navit *this_)
566 struct navit_vehicle *nv;
567 transform_copy(this_->trans, this_->trans_cursor);
571 navit_vehicle_draw(this_, nv, NULL);
577 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
579 struct coord c1, c2, *center;
580 if (scale < this_->zoom_min)
581 scale=this_->zoom_min;
582 if (scale > this_->zoom_max)
583 scale=this_->zoom_max;
585 transform_reverse(this_->trans, p, &c1);
586 transform_set_scale(this_->trans, scale);
588 transform_reverse(this_->trans, p, &c2);
589 center = transform_center(this_->trans);
590 center->x += c1.x - c2.x;
591 center->y += c1.y - c2.y;
598 * @brief Automatically adjusts zoom level
600 * This function automatically adjusts the current
601 * zoom level according to the current speed.
603 * @param this_ The navit struct
604 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
605 * @param speed The vehicles speed in meters per second
606 * @param dir The direction into which the vehicle moves
609 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
616 if (! this_->autozoom_active) {
620 distance = speed * this_->autozoom_secs;
622 transform_get_size(this_->trans, &w, &h);
623 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
624 scale = transform_get_scale(this_->trans);
626 /* We make sure that the point we want to see is within a certain range
627 * around the vehicle. The radius of this circle is the size of the
628 * screen. This doesn't necessarily mean the point is visible because of
629 * perspective etc. Quite rough, but should be enough. */
632 new_scale = (double)distance / h * 16;
634 new_scale = (double)distance / w * 16;
637 if (abs(new_scale - scale) < 2) {
641 if (new_scale >= this_->autozoom_min) {
642 navit_scale(this_, (long)new_scale, &pc, 0);
644 if (scale != this_->autozoom_min) {
645 navit_scale(this_, this_->autozoom_min, &pc, 0);
651 * Change the current zoom level, zooming closer to the ground
653 * @param navit The navit instance
654 * @param factor The zoom factor, usually 2
655 * @param p The invariant point (if set to NULL, default to center)
659 navit_zoom_in(struct navit *this_, int factor, struct point *p)
661 long scale=transform_get_scale(this_->trans)/factor;
664 navit_scale(this_, scale, p, 1);
668 * Change the current zoom level
670 * @param navit The navit instance
671 * @param factor The zoom factor, usually 2
672 * @param p The invariant point (if set to NULL, default to center)
676 navit_zoom_out(struct navit *this_, int factor, struct point *p)
678 long scale=transform_get_scale(this_->trans)*factor;
679 navit_scale(this_, scale, p, 1);
683 navit_zoom_in_cursor(struct navit *this_, int factor)
686 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
687 navit_zoom_in(this_, factor, &p);
688 this_->vehicle->follow_curr=this_->vehicle->follow;
690 navit_zoom_in(this_, factor, NULL);
694 navit_zoom_out_cursor(struct navit *this_, int factor)
697 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
698 navit_zoom_out(this_, 2, &p);
699 this_->vehicle->follow_curr=this_->vehicle->follow;
701 navit_zoom_out(this_, 2, NULL);
705 navit_cmd_zoom_in(struct navit *this_)
707 navit_zoom_in_cursor(this_, 2);
712 navit_cmd_zoom_out(struct navit *this_)
714 navit_zoom_out_cursor(this_, 2);
720 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
722 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
723 navit_say(this, in[0]->u.str);
726 static GHashTable *cmd_int_var_hash = NULL;
729 * Store key value pair for the command system
731 * @param navit The navit instance
732 * @param function unused (needed to match command function signiture)
733 * @param in input attributes in[0] is the key string, in[1] is the integer value to store
734 * @param out output attributes, unused
735 * @param valid unused
739 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
741 if(!cmd_int_var_hash) {
742 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
745 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
746 (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
748 struct attr*val = g_new(struct attr,1);
749 attr_dup_content(in[1],val);
750 key = g_strdup(in[0]->u.str);
751 g_hash_table_insert(cmd_int_var_hash, key, val);
756 * command to toggle the active state of a named layer of the current layout
758 * @param navit The navit instance
759 * @param function unused (needed to match command function signiture)
760 * @param in input attribute in[0] is the name of the layer
761 * @param out output unused
762 * @param valid unused
766 navit_cmd_toggle_layer(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
768 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
769 if(this->layout_current && this->layout_current->layers) {
770 GList* layers = this->layout_current->layers;
772 struct layer*l=layers->data;
773 if(l && !strcmp(l->name,in[0]->u.str) ) {
778 layers=g_list_next(layers);
786 * Get value given a key string for the command system
788 * @param navit The navit instance
789 * @param function unused (needed to match command function signiture)
790 * @param in input attribute in[0] is the key string
791 * @param out output attribute, the value for the given key string if exists or 0
792 * @param valid unused
796 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
798 struct attr **list = g_new0(struct attr *,2);
799 if(!cmd_int_var_hash) {
800 struct attr*val = g_new0(struct attr,1);
801 val->type = attr_type_int_begin;
805 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
806 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
811 struct attr*val = g_new0(struct attr,1);
812 val->type = attr_type_int_begin;
821 GList *cmd_int_var_stack = NULL;
824 * Push an integer to the stack for the command system
826 * @param navit The navit instance
827 * @param function unused (needed to match command function signiture)
828 * @param in input attribute in[0] is the integer attibute to push
829 * @param out output attributes, unused
830 * @param valid unused
834 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
836 if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
837 struct attr*val = g_new(struct attr,1);
838 attr_dup_content(in[0],val);
839 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
844 * Pop an integer from the command system's integer stack
846 * @param navit The navit instance
847 * @param function unused (needed to match command function signiture)
848 * @param in input attributes unused
849 * @param out output attribute, the value popped if stack isn't empty or 0
850 * @param valid unused
854 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
856 struct attr **list = g_new0(struct attr *,2);
857 if(!cmd_int_var_stack) {
858 struct attr*val = g_new0(struct attr,1);
859 val->type = attr_type_int_begin;
864 list[0] = cmd_int_var_stack->data;
865 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
872 * Get current size of command system's integer stack
874 * @param navit The navit instance
875 * @param function unused (needed to match command function signiture)
876 * @param in input attributes unused
877 * @param out output attribute, the size of stack
878 * @param valid unused
882 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
885 struct attr *attr = g_new0(struct attr ,1);
886 attr->type = attr_type_int_begin;
887 if(!cmd_int_var_stack) {
891 attr->u.num = g_list_length(cmd_int_var_stack);
893 list = g_new0(struct attr *,2);
897 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
901 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
904 char *description=NULL;
909 pc.pro = transform_get_projection(this->trans);
910 if (ATTR_IS_COORD(in[0]->type)) {
911 pc.x=in[0]->u.coord->x;
912 pc.y=in[0]->u.coord->y;
914 } else if (ATTR_IS_PCOORD(in[0]->type)) {
917 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
922 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
928 if (in[0] && ATTR_IS_STRING(in[0]->type))
929 description=in[0]->u.str;
930 navit_set_destination(this, &pc, description, 1);
934 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
937 attr.type=attr_type_string_begin;
940 *out=attr_generic_add_attr(*out, &attr);
944 static struct command_table commands[] = {
945 {"zoom_in",command_cast(navit_cmd_zoom_in)},
946 {"zoom_out",command_cast(navit_cmd_zoom_out)},
947 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
948 {"say",command_cast(navit_cmd_say)},
949 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
950 {"set_destination",command_cast(navit_cmd_set_destination)},
951 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
952 {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
953 {"set_int_var",command_cast(navit_cmd_set_int_var)},
954 {"get_int_var",command_cast(navit_cmd_get_int_var)},
955 {"push_int",command_cast(navit_cmd_push_int)},
956 {"pop_int",command_cast(navit_cmd_pop_int)},
957 {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
958 {"toggle_layer",command_cast(navit_cmd_toggle_layer)},
962 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
964 command_add_table(this_->attr_cbl, commands, count, this_);
968 navit_new(struct attr *parent, struct attr **attrs)
970 struct navit *this_=g_new0(struct navit, 1);
971 struct pcoord center;
974 enum projection pro=projection_mg;
979 this_->self.type=attr_navit;
980 this_->self.u.navit=this_;
981 this_->attr_cbl=callback_list_new();
983 this_->orientation=-1;
984 this_->tracking_flag=1;
985 this_->recentdest_count=10;
986 this_->osd_configuration=-1;
988 this_->center_timeout = 10;
989 this_->use_mousewheel = 1;
990 this_->autozoom_secs = 10;
991 this_->autozoom_min = 7;
992 this_->autozoom_active = 0;
994 this_->zoom_max = 2097152;
995 this_->follow_cursor = 1;
999 this_->trans = transform_new();
1000 this_->trans_cursor = transform_new();
1001 transform_from_geo(pro, &g, &co);
1006 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
1008 this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
1012 for (;*attrs; attrs++) {
1013 navit_set_attr_do(this_, *attrs, 1);
1015 this_->displaylist=graphics_displaylist_new();
1016 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
1018 this_->messages = messagelist_new(attrs);
1024 navit_set_gui(struct navit *this_, struct gui *gui)
1029 if (gui_has_main_loop(this_->gui)) {
1030 if (! main_loop_gui) {
1031 main_loop_gui=this_->gui;
1033 dbg(0,"gui with main loop already active, ignoring this instance");
1041 navit_add_message(struct navit *this_, char *message)
1043 message_new(this_->messages, message);
1047 *navit_get_messages(struct navit *this_)
1049 return message_get(this_->messages);
1053 navit_set_graphics(struct navit *this_, struct graphics *gra)
1058 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1059 graphics_add_callback(gra, this_->resize_callback);
1060 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1061 graphics_add_callback(gra, this_->button_callback);
1062 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1063 graphics_add_callback(gra, this_->motion_callback);
1064 this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1065 graphics_add_callback(gra, this_->predraw_callback);
1070 navit_get_graphics(struct navit *this_)
1075 struct vehicleprofile *
1076 navit_get_vehicleprofile(struct navit *this_)
1078 return this_->vehicleprofile;
1082 navit_get_vehicleprofiles(struct navit *this_)
1084 return this_->vehicleprofiles;
1088 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1093 c=transform_center(this_->trans);
1094 transform_to_geo(transform_get_projection(this_->trans), c, &g);
1095 transform_set_projection(this_->trans, pro);
1096 transform_from_geo(pro, &g, c);
1102 * Start the route computing to a given set of coordinates
1104 * @param navit The navit instance
1105 * @param c The coordinate to start routing to
1106 * @param description A label which allows the user to later identify this destination in the former destinations selection
1110 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1112 char *destination_file;
1114 this_->destination=*c;
1115 this_->destination_valid=1;
1117 dbg(1, "navit->navit_set_destination %i\n", c->x);
1118 dbg(1, "navit->navit_set_destination %i\n", c->y);
1121 this_->destination_valid=0;
1122 destination_file = bookmarks_get_destination_file(TRUE);
1123 bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
1124 g_free(destination_file);
1125 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1127 route_set_destination(this_->route, c, async);
1129 if (this_->ready == 3)
1135 * Start the route computing to a given set of coordinates including waypoints
1137 * @param navit The navit instance
1138 * @param c The coordinate to start routing to
1139 * @param description A label which allows the user to later identify this destination in the former destinations selection
1143 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1145 char *destination_file;
1147 this_->destination=c[count-1];
1148 this_->destination_valid=1;
1150 this_->destination_valid=0;
1151 destination_file = bookmarks_get_destination_file(TRUE);
1152 bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
1153 g_free(destination_file);
1154 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1156 route_set_destinations(this_->route, c, count, async);
1158 if (this_->ready == 3)
1164 * @brief Checks if a route is calculated
1166 * This function checks if a route is calculated.
1168 * @param this_ The navit struct whose route should be checked.
1169 * @return True if the route is set, false otherwise.
1172 navit_check_route(struct navit *this_)
1175 return route_get_path_set(this_->route);
1182 navit_former_destinations_active(struct navit *this_)
1184 char *destination_file = bookmarks_get_destination_file(FALSE);
1188 f=fopen(destination_file,"r");
1190 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
1194 g_free(destination_file);
1199 navit_add_former_destinations_from_file(struct navit *this_)
1201 char *destination_file = bookmarks_get_destination_file(FALSE);
1202 struct attr *attrs[4];
1203 struct map_rect *mr;
1205 int i,valid=0,count=0;
1207 struct pcoord pc[16];
1213 parent.type=attr_navit;
1214 parent.u.navit=this_;
1216 type.type=attr_type;
1217 type.u.str="textfile";
1219 data.type=attr_data;
1220 data.u.str=destination_file;
1222 flags.type=attr_flags;
1225 attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1227 this_->former_destination=map_new(&parent, attrs);
1228 g_free(destination_file);
1229 if (!this_->route || !navit_former_destinations_active(this_))
1231 mr=map_rect_new(this_->former_destination, NULL);
1232 while ((item=map_rect_get_item(mr))) {
1233 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
1236 map_rect_destroy(mr);
1237 if (valid && count > 0) {
1238 for (i = 0 ; i < count ; i++) {
1239 pc[i].pro=map_projection(this_->former_destination);
1244 route_set_destination(this_->route, &pc[0], 1);
1246 route_set_destinations(this_->route, pc, count, 1);
1247 this_->destination=pc[count-1];
1248 this_->destination_valid=1;
1254 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1259 if (this_->textfile_debug_log && this_->vehicle) {
1260 str1=g_strdup_vprintf(fmt, ap);
1261 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1262 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1270 navit_textfile_debug_log_at(struct navit *this_, struct pcoord *pc, const char *fmt, ...)
1275 if (this_->textfile_debug_log && this_->vehicle) {
1276 str1=g_strdup_vprintf(fmt, ap);
1277 str2=g_strdup_printf("0x%x 0x%x%s%s\n", pc->x, pc->y, strlen(str1) ? " " : "", str1);
1278 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1286 navit_say(struct navit *this_, char *text)
1289 speech_say(this_->speech, text);
1294 * @brief Toggles the navigation announcer for navit
1295 * @param this_ The navit object
1298 navit_cmd_announcer_toggle(struct navit *this_)
1300 struct attr attr, speechattr;
1302 // search for the speech attribute
1303 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1305 // find out if the corresponding attribute attr_active has been set
1306 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1308 attr.u.num = !attr.u.num;
1310 // otherwise disable it because voice is enabled by default
1311 attr.type = attr_active;
1315 // apply the new state
1316 if(!speech_set_attr(speechattr.u.speech, &attr))
1319 // announce that the speech attribute has changed
1320 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1324 navit_speak(struct navit *this_)
1326 struct navigation *nav=this_->navigation;
1327 struct map *map=NULL;
1328 struct map_rect *mr=NULL;
1332 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1334 dbg(1, "this_.speech->active %i\n", attr.u.num);
1339 map=navigation_get_map(nav);
1341 mr=map_rect_new(map, NULL);
1343 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1344 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1345 speech_say(this_->speech, attr.u.str);
1346 navit_add_message(this_, attr.u.str);
1347 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1349 map_rect_destroy(mr);
1354 navit_window_roadbook_update(struct navit *this_)
1356 struct navigation *nav=this_->navigation;
1357 struct map *map=NULL;
1358 struct map_rect *mr=NULL;
1361 struct param_list param[5];
1365 datawindow_mode(this_->roadbook_window, 1);
1367 map=navigation_get_map(nav);
1369 mr=map_rect_new(map, NULL);
1370 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1372 dbg(0,"while loop\n");
1373 while ((item=map_rect_get_item(mr))) {
1374 dbg(0,"item=%p\n", item);
1376 if (item->type != type_nav_position) {
1377 item_attr_get(item, attr_navigation_long, &attr);
1378 if (attr.u.str == NULL) {
1381 dbg(2, "Command='%s'\n", attr.u.str);
1382 param[0].value=g_strdup(attr.u.str);
1384 param[0].value=_("Position");
1385 param[0].name=_("Command");
1387 item_attr_get(item, attr_length, &attr);
1388 dbg(2, "Length=%d\n", attr.u.num);
1389 param[1].name=_("Length");
1391 if ( attr.u.num >= 2000 )
1393 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1397 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1400 item_attr_get(item, attr_time, &attr);
1401 dbg(2, "Time=%d\n", attr.u.num);
1403 param[2].name=_("Time");
1406 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1410 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1413 item_attr_get(item, attr_destination_length, &attr);
1414 dbg(2, "Destlength=%d\n", attr.u.num);
1415 param[3].name=_("Destination Length");
1416 if ( attr.u.num >= 2000 )
1418 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1422 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1425 item_attr_get(item, attr_destination_time, &attr);
1426 dbg(2, "Desttime=%d\n", attr.u.num);
1428 param[4].name=_("Destination Time");
1431 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1435 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1437 datawindow_add(this_->roadbook_window, param, 5);
1439 map_rect_destroy(mr);
1441 datawindow_mode(this_->roadbook_window, 0);
1445 navit_window_roadbook_destroy(struct navit *this_)
1448 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1449 this_->roadbook_window=NULL;
1450 this_->roadbook_callback=NULL;
1453 navit_window_roadbook_new(struct navit *this_)
1455 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1459 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1460 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1461 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1462 navit_window_roadbook_update(this_);
1466 navit_init(struct navit *this_)
1473 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1475 if (!this_->gui && !(this_->flags & 2)) {
1477 navit_destroy(this_);
1480 if (!this_->gra && !(this_->flags & 1)) {
1481 dbg(0,"no graphics\n");
1482 navit_destroy(this_);
1485 dbg(2,"Connecting gui to graphics\n");
1486 if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1487 struct attr attr_type_gui, attr_type_graphics;
1488 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1489 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1490 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1491 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1492 dbg(0," for explanations and solutions\n");
1494 navit_destroy(this_);
1497 if (this_->speech && this_->navigation) {
1499 speech.type=attr_speech;
1500 speech.u.speech=this_->speech;
1501 navigation_set_attr(this_->navigation, &speech);
1503 dbg(2,"Initializing graphics\n");
1504 dbg(2,"Setting Vehicle\n");
1505 navit_set_vehicle(this_, this_->vehicle);
1506 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1507 if (this_->mapsets) {
1508 struct mapset_handle *msh;
1509 ms=this_->mapsets->data;
1510 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1511 msh=mapset_open(ms);
1512 while (msh && (map=mapset_next(msh, 0))) {
1513 map_add_callback(map, this_->progress_cb);
1518 if ((map=route_get_map(this_->route))) {
1520 map_a.type=attr_map;
1522 mapset_add_attr(ms, &map_a);
1524 if ((map=route_get_graph_map(this_->route))) {
1525 struct attr map_a,active;
1526 map_a.type=attr_map;
1528 active.type=attr_active;
1530 mapset_add_attr(ms, &map_a);
1531 map_set_attr(map, &active);
1533 route_set_mapset(this_->route, ms);
1534 route_set_projection(this_->route, transform_get_projection(this_->trans));
1536 if (this_->tracking) {
1537 tracking_set_mapset(this_->tracking, ms);
1539 tracking_set_route(this_->tracking, this_->route);
1541 if (this_->navigation) {
1542 if ((map=navigation_get_map(this_->navigation))) {
1543 struct attr map_a,active;
1544 map_a.type=attr_map;
1546 active.type=attr_active;
1548 mapset_add_attr(ms, &map_a);
1549 map_set_attr(map, &active);
1552 if (this_->tracking) {
1553 if ((map=tracking_get_map(this_->tracking))) {
1554 struct attr map_a,active;
1555 map_a.type=attr_map;
1557 active.type=attr_active;
1559 mapset_add_attr(ms, &map_a);
1560 map_set_attr(map, &active);
1563 navit_add_former_destinations_from_file(this_);
1566 struct attr callback;
1567 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1568 callback.type=attr_callback;
1569 callback.u.callback=this_->route_cb;
1570 route_add_attr(this_->route, &callback);
1572 if (this_->navigation) {
1573 if (this_->speech) {
1574 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1575 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1578 navigation_set_route(this_->navigation, this_->route);
1580 dbg(2,"Setting Center\n");
1581 center_file = bookmarks_get_center_file(FALSE);
1582 bookmarks_set_center_from_file(this_->bookmarks, center_file);
1583 g_free(center_file);
1585 if (this_->menubar) {
1586 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1588 navit_add_menu_windows_items(this_, men);
1594 navit_window_roadbook_new(this_);
1595 navit_window_items_new(this_);
1598 messagelist_init(this_->messages);
1600 navit_set_cursors(this_);
1602 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1603 callback=(this_->ready == 2);
1605 dbg(2,"ready=%d\n",this_->ready);
1606 if (this_->ready == 3)
1607 navit_draw_async(this_, 1);
1609 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1611 routech_test(this_);
1616 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1621 c.x=(r->rl.x+r->lu.x)/2;
1622 c.y=(r->rl.y+r->lu.y)/2;
1623 transform_set_center(this_->trans, &c);
1624 dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1625 while (scale < 1<<20) {
1627 transform_set_scale(this_->trans, scale);
1628 transform_setup_source_rect(this_->trans);
1629 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1630 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1631 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1632 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1633 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1639 if (this_->ready == 3)
1640 navit_draw_async(this_,0);
1644 navit_zoom_to_route(struct navit *this_, int orientation)
1647 struct map_rect *mr=NULL;
1650 struct coord_rect r;
1655 map=route_get_map(this_->route);
1656 dbg(1,"map=%p\n",map);
1658 mr=map_rect_new(map, NULL);
1659 dbg(1,"mr=%p\n",mr);
1661 while ((item=map_rect_get_item(mr))) {
1662 dbg(1,"item=%s\n", item_to_name(item->type));
1663 while (item_coord_get(item, &c, 1)) {
1668 coord_rect_extend(&r, &c);
1675 if (orientation != -1)
1676 transform_set_yaw(this_->trans, orientation);
1677 navit_zoom_to_rect(this_, &r);
1681 navit_cmd_zoom_to_route(struct navit *this)
1683 navit_zoom_to_route(this, 0);
1688 * Change the current zoom level
1690 * @param navit The navit instance
1691 * @param center The point where to center the map, including its projection
1695 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1697 struct coord *c=transform_center(this_->trans);
1699 enum projection pro = transform_get_projection(this_->trans);
1700 if (pro != center->pro) {
1703 transform_from_to(&c1, center->pro, &c2, pro);
1710 navit_set_timeout(this_);
1711 if (this_->ready == 3)
1716 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1720 transform_set_center(this_->trans, c);
1721 transform_get_size(this_->trans, &width, &height);
1724 update_transformation(this_->trans, &po, p, NULL);
1726 navit_set_timeout(this_);
1730 * Links all vehicles to a cursor depending on the current profile.
1732 * @param this_ A navit instance
1733 * @author Ralph Sennhauser (10/2009)
1736 navit_set_cursors(struct navit *this_)
1739 struct navit_vehicle *nv;
1743 v=g_list_first(this_->vehicles); // GList of navit_vehicles
1746 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1747 if (!strcmp(name.u.str,"none"))
1750 c=layout_get_cursor(this_->layout_current, name.u.str);
1752 c=layout_get_cursor(this_->layout_current, "default");
1753 vehicle_set_cursor(nv->vehicle, c);
1760 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1763 struct navit_vehicle *nv=this_->vehicle;
1765 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
1766 #if 0 /* Better improve track.c to get that issue resolved or make it configurable with being off the default, the jumping back to the center is a bit annoying */
1767 float min_offset = 0.; // Percent offset at min_offset_speed.
1768 float max_offset = 30.; // Percent offset at max_offset_speed.
1769 int min_offset_speed = 2; // Speed in km/h
1770 int max_offset_speed = 50; // Speed ini km/h
1771 // Calculate cursor offset from the center of the screen, upon speed.
1772 if (nv->speed <= min_offset_speed) {
1773 offset = min_offset;
1774 } else if (nv->speed > max_offset_speed) {
1775 offset = max_offset;
1777 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1781 transform_get_size(this_->trans, &width, &height);
1782 if (this_->orientation == -1 || keep_orientation) {
1784 p->y=(50 + offset)*height/100;
1786 *dir=keep_orientation?this_->orientation:nv->dir;
1789 if (this_->tracking && this_->tracking_flag) {
1790 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1792 mdir=nv->dir-this_->orientation;
1795 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1796 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1798 *dir=this_->orientation;
1804 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
1808 struct navit_vehicle *nv=this_->vehicle;
1809 navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
1810 transform_set_yaw(this_->trans, dir);
1811 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1813 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1817 navit_set_center_cursor_draw(struct navit *this_)
1819 navit_set_center_cursor(this_,1,0);
1820 if (this_->ready == 3)
1821 navit_draw_async(this_, 1);
1825 navit_cmd_set_center_cursor(struct navit *this_)
1827 navit_set_center_cursor_draw(this_);
1831 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1835 transform_reverse(this_->trans, p, &c);
1838 pc.pro = transform_get_projection(this_->trans);
1839 navit_set_center(this_, &pc, set_timeout);
1843 switch((*attrs)->type) {
1845 zoom=(*attrs)->u.num;
1848 g=*((*attrs)->u.coord_geo);
1853 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1855 int dir=0, orient_old=0, attr_updated=0;
1859 struct navit_vehicle *nv;
1862 active.type=attr_active;
1865 switch (attr->type) {
1867 attr_updated=(this_->autozoom_secs != attr->u.num);
1868 this_->autozoom_secs = attr->u.num;
1870 case attr_autozoom_active:
1871 attr_updated=(this_->autozoom_active != attr->u.num);
1872 this_->autozoom_active = attr->u.num;
1875 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1876 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1877 transform_set_center(this_->trans, &co);
1879 case attr_drag_bitmap:
1880 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1881 this_->drag_bitmap=!!attr->u.num;
1884 attr_updated=(this_->flags != attr->u.num);
1885 this_->flags=attr->u.num;
1887 case attr_flags_graphics:
1888 attr_updated=(this_->graphics_flags != attr->u.num);
1889 this_->graphics_flags=attr->u.num;
1892 if (!this_->vehicle)
1894 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1895 this_->vehicle->follow_curr = attr->u.num;
1898 if(this_->layout_current!=attr->u.layout) {
1899 this_->layout_current=attr->u.layout;
1900 graphics_font_destroy_all(this_->gra);
1901 navit_set_cursors(this_);
1902 if (this_->ready == 3)
1907 case attr_layout_name:
1911 if (!strcmp(lay->name,attr->u.str)) {
1913 attr.type=attr_layout;
1915 return navit_set_attr_do(this_, &attr, init);
1920 case attr_map_border:
1921 if (this_->border != attr->u.num) {
1922 this_->border=attr->u.num;
1926 case attr_orientation:
1927 orient_old=this_->orientation;
1928 this_->orientation=attr->u.num;
1930 if (this_->orientation != -1) {
1931 dir = this_->orientation;
1933 if (this_->vehicle) {
1934 dir = this_->vehicle->dir;
1937 transform_set_yaw(this_->trans, dir);
1938 if (orient_old != this_->orientation) {
1940 if (this_->ready == 3)
1947 case attr_osd_configuration:
1948 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1949 attr_updated=(this_->osd_configuration != attr->u.num);
1950 this_->osd_configuration=attr->u.num;
1953 attr_updated=(this_->pitch != attr->u.num);
1954 this_->pitch=attr->u.num;
1955 transform_set_pitch(this_->trans, this_->pitch);
1956 if (!init && attr_updated && this_->ready == 3)
1959 case attr_projection:
1960 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1961 navit_projection_set(this_, attr->u.projection, !init);
1966 attr_updated=(this_->radius != attr->u.num);
1967 this_->radius=attr->u.num;
1969 case attr_recent_dest:
1970 attr_updated=(this_->recentdest_count != attr->u.num);
1971 this_->recentdest_count=attr->u.num;
1974 if(this_->speech && this_->speech != attr->u.speech) {
1976 this_->speech = attr->u.speech;
1980 attr_updated=(this_->center_timeout != attr->u.num);
1981 this_->center_timeout = attr->u.num;
1984 attr_updated=(this_->tracking_flag != !!attr->u.num);
1985 this_->tracking_flag=!!attr->u.num;
1987 case attr_transformation:
1988 this_->trans=attr->u.transformation;
1990 case attr_use_mousewheel:
1991 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1992 this_->use_mousewheel=!!attr->u.num;
1998 if (nv->vehicle == attr->u.vehicle) {
1999 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2001 vehicle_set_attr(this_->vehicle->vehicle, &active);
2003 vehicle_set_attr(nv->vehicle, &active);
2006 navit_set_vehicle(this_, nv);
2012 zoom=transform_get_scale(this_->trans);
2013 attr_updated=(zoom != attr->u.num);
2014 transform_set_scale(this_->trans, attr->u.num);
2015 if (attr_updated && !init)
2019 attr_updated=(attr->u.num != this_->zoom_min);
2020 this_->zoom_min=attr->u.num;
2023 attr_updated=(attr->u.num != this_->zoom_max);
2024 this_->zoom_max=attr->u.num;
2027 navit_add_message(this_, attr->u.str);
2029 case attr_follow_cursor:
2030 attr_updated=(this_->follow_cursor != !!attr->u.num);
2031 this_->follow_cursor=!!attr->u.num;
2034 attr_updated=(this_->imperial != attr->u.num);
2035 this_->imperial=attr->u.num;
2040 if (attr_updated && !init) {
2041 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2042 if (attr->type == attr_osd_configuration)
2043 graphics_draw_mode(this_->gra, draw_mode_end);
2049 navit_set_attr(struct navit *this_, struct attr *attr)
2051 return navit_set_attr_do(this_, attr, 0);
2055 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2057 struct message *msg;
2063 msg = navit_get_messages(this_);
2071 len += strlen(msg->text) + 1;
2074 attr->u.str = g_malloc(len + 1);
2076 msg = navit_get_messages(this_);
2079 g_stpcpy((attr->u.str + offset), msg->text);
2080 offset += strlen(msg->text);
2081 attr->u.str[offset] = '\n';
2087 attr->u.str[len] = '\0';
2090 attr->u.num=this_->imperial;
2092 case attr_bookmark_map:
2093 attr->u.map=bookmarks_get_map(this_->bookmarks);
2095 case attr_bookmarks:
2096 attr->u.bookmarks=this_->bookmarks;
2098 case attr_callback_list:
2099 attr->u.callback_list=this_->attr_cbl;
2101 case attr_destination:
2102 if (! this_->destination_valid)
2104 attr->u.pcoord=&this_->destination;
2106 case attr_displaylist:
2107 attr->u.displaylist=this_->displaylist;
2108 return (attr->u.displaylist != NULL);
2110 if (!this_->vehicle)
2112 attr->u.num=this_->vehicle->follow_curr;
2114 case attr_former_destination_map:
2115 attr->u.map=this_->former_destination;
2118 attr->u.graphics=this_->gra;
2119 ret=(attr->u.graphics != NULL);
2122 attr->u.gui=this_->gui;
2123 ret=(attr->u.gui != NULL);
2128 iter->u.list=g_list_next(iter->u.list);
2130 iter->u.list=this_->layouts;
2134 attr->u.layout=(struct layout *)iter->u.list->data;
2136 attr->u.layout=this_->layout_current;
2140 if (iter && this_->mapsets) {
2141 if (!iter->u.mapset_handle) {
2142 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2144 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2146 mapset_close(iter->u.mapset_handle);
2154 attr->u.mapset=this_->mapsets->data;
2155 ret=(attr->u.mapset != NULL);
2157 case attr_navigation:
2158 attr->u.navigation=this_->navigation;
2160 case attr_orientation:
2161 attr->u.num=this_->orientation;
2163 case attr_osd_configuration:
2164 attr->u.num=this_->osd_configuration;
2167 attr->u.num=transform_get_pitch(this_->trans);
2169 case attr_projection:
2171 attr->u.num=transform_get_projection(this_->trans);
2177 attr->u.route=this_->route;
2180 attr->u.speech=this_->speech;
2183 attr->u.num=this_->tracking_flag;
2185 case attr_trackingo:
2186 attr->u.tracking=this_->tracking;
2188 case attr_transformation:
2189 attr->u.transformation=this_->trans;
2194 iter->u.list=g_list_next(iter->u.list);
2196 iter->u.list=this_->vehicles;
2200 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2202 if(this_->vehicle) {
2203 attr->u.vehicle=this_->vehicle->vehicle;
2209 case attr_vehicleprofile:
2210 attr->u.vehicleprofile=this_->vehicleprofile;
2213 attr->u.num=transform_get_scale(this_->trans);
2215 case attr_autozoom_active:
2216 attr->u.num=this_->autozoom_active;
2218 case attr_follow_cursor:
2219 attr->u.num=this_->follow_cursor;
2229 navit_add_log(struct navit *this_, struct log *log)
2231 struct attr type_attr;
2232 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2234 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2235 char *header = "type=track_tracked\n";
2236 if (this_->textfile_debug_log)
2238 log_set_header(log, header, strlen(header));
2239 this_->textfile_debug_log=log;
2246 navit_add_attr(struct navit *this_, struct attr *attr)
2249 switch (attr->type) {
2251 navit_add_callback(this_, attr->u.callback);
2254 ret=navit_add_log(this_, attr->u.log);
2257 ret=navit_set_gui(this_, attr->u.gui);
2260 ret=navit_set_graphics(this_, attr->u.graphics);
2263 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2264 if(!this_->layout_current)
2265 this_->layout_current=attr->u.layout;
2268 this_->route=attr->u.route;
2271 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2273 case attr_navigation:
2274 this_->navigation=attr->u.navigation;
2276 case attr_recent_dest:
2277 this_->recentdest_count = attr->u.num;
2280 this_->speech=attr->u.speech;
2283 this_->tracking=attr->u.tracking;
2286 ret=navit_add_vehicle(this_, attr->u.vehicle);
2288 case attr_vehicleprofile:
2289 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2291 case attr_autozoom_min:
2292 this_->autozoom_min = attr->u.num;
2297 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2302 navit_remove_attr(struct navit *this_, struct attr *attr)
2305 switch (attr->type) {
2307 navit_remove_callback(this_, attr->u.callback);
2316 navit_attr_iter_new(void)
2318 return g_new0(struct attr_iter, 1);
2322 navit_attr_iter_destroy(struct attr_iter *iter)
2328 navit_add_callback(struct navit *this_, struct callback *cb)
2330 callback_list_add(this_->attr_cbl, cb);
2334 navit_remove_callback(struct navit *this_, struct callback *cb)
2336 callback_list_remove(this_->attr_cbl, cb);
2340 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2342 * @param navit The navit instance
2347 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2349 struct point cursor_pnt;
2350 enum projection pro;
2357 pro=transform_get_projection(this_->trans_cursor);
2360 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2362 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2367 pro=transform_get_projection(this_->trans);
2368 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2371 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2373 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2379 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2381 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2382 struct pcoord cursor_pc;
2383 struct point cursor_pnt, *pnt=&cursor_pnt;
2384 struct tracking *tracking=NULL;
2385 struct pcoord pc[16];
2386 enum projection pro=transform_get_projection(this_->trans_cursor);
2388 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2390 char *destination_file;
2393 if (this_->ready != 3) {
2394 profile(0,"return 1\n");
2397 navit_layout_switch(this_);
2398 if (this_->vehicle == nv && this_->tracking_flag)
2399 tracking=this_->tracking;
2401 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2402 attr_object=tracking;
2403 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2405 attr_object=nv->vehicle;
2406 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2408 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2409 if (!attr_valid.u.num != attr_position_valid_invalid)
2411 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2412 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2413 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2414 profile(0,"return 2\n");
2417 nv->dir=*attr_dir.u.numd;
2418 nv->speed=*attr_speed.u.numd;
2419 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2420 if (nv != this_->vehicle) {
2421 navit_vehicle_draw(this_, nv, NULL);
2422 profile(0,"return 3\n");
2425 cursor_pc.x = nv->coord.x;
2426 cursor_pc.y = nv->coord.y;
2427 cursor_pc.pro = pro;
2430 route_set_position_from_tracking(this_->route, tracking, pro);
2432 route_set_position(this_->route, &cursor_pc);
2434 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2435 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2436 if (this_->gui && nv->speed > 2)
2437 navit_disable_suspend();
2439 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2440 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2441 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2442 navit_set_center_cursor_draw(this_);
2444 navit_vehicle_draw(this_, nv, pnt);
2446 if (nv->follow_curr > 1)
2449 nv->follow_curr=nv->follow;
2450 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2452 /* Finally, if we reached our destination, stop navigation. */
2454 switch(route_destination_reached(this_->route)) {
2456 route_remove_waypoint(this_->route);
2457 count=route_get_destinations(this_->route, pc, 16);
2458 destination_file = bookmarks_get_destination_file(TRUE);
2459 bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2462 navit_set_destination(this_, NULL, NULL, 0);
2466 profile(0,"return 5\n");
2470 * Set the position of the vehicle
2472 * @param navit The navit instance
2473 * @param c The coordinate to set as position
2478 navit_set_position(struct navit *this_, struct pcoord *c)
2481 route_set_position(this_->route, c);
2482 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2484 if (this_->ready == 3)
2489 navit_set_vehicleprofile(struct navit *this_, char *name)
2493 l=this_->vehicleprofiles;
2495 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2496 if (!strcmp(attr.u.str, name)) {
2497 this_->vehicleprofile=l->data;
2499 route_set_profile(this_->route, this_->vehicleprofile);
2509 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2513 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2514 if (navit_set_vehicleprofile(this_, attr.u.str))
2517 if (!navit_set_vehicleprofile(this_,"car")) {
2518 /* We do not have a fallback "car" profile
2519 * so lets set any profile */
2521 l=this_->vehicleprofiles;
2523 this_->vehicleprofile=l->data;
2525 route_set_profile(this_->route, this_->vehicleprofile);
2531 * Register a new vehicle
2533 * @param navit The navit instance
2534 * @param v The vehicle instance
2535 * @returns 1 for success
2538 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2540 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2541 struct attr follow, active, animate;
2546 nv->animate_cursor=0;
2547 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2548 nv->follow=follow.u.num;
2549 nv->follow_curr=nv->follow;
2550 this_->vehicles=g_list_append(this_->vehicles, nv);
2551 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2552 navit_set_vehicle(this_, nv);
2553 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2554 nv->animate_cursor=animate.u.num;
2555 nv->callback.type=attr_callback;
2556 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2557 vehicle_add_attr(nv->vehicle, &nv->callback);
2558 vehicle_set_attr(nv->vehicle, &this_->self);
2566 navit_get_gui(struct navit *this_)
2571 struct transformation *
2572 navit_get_trans(struct navit *this_)
2574 return this_->trans;
2578 navit_get_route(struct navit *this_)
2580 return this_->route;
2584 navit_get_navigation(struct navit *this_)
2586 return this_->navigation;
2589 struct displaylist *
2590 navit_get_displaylist(struct navit *this_)
2592 return this_->displaylist;
2596 navit_layout_switch(struct navit *n)
2600 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2601 double trise,tset,trise_actual;
2603 int year, month, day;
2605 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2606 return; //No layout - nothing to switch
2610 l=layout_attr.u.layout;
2612 if (l->dayname || l->nightname) {
2613 //Ok, we know that we have profile to switch
2615 //Check that we aren't calculating too fast
2616 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2617 currTs=iso8601_to_secs(iso8601_attr.u.str);
2618 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2620 if (currTs-(n->prevTs)<60) {
2621 //We've have to wait a little
2624 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2626 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2627 return; //No valid fix yet
2629 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2630 //No position - no sun
2634 //We calculate sunrise anyway, cause it is needed both for day and for night
2635 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2636 //near the pole sun never rises/sets, so we should never switch profiles
2637 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2643 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2646 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
2647 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2648 //The sun is rising now!
2649 if (strcmp(l->name,l->dayname)) {
2650 navit_set_layout_by_name(n,l->dayname);
2655 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2656 //near the pole sun never rises/sets, so we should never switch profiles
2657 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2661 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2662 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2663 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
2664 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2666 if (strcmp(l->name,l->nightname)) {
2667 navit_set_layout_by_name(n,l->nightname);
2677 navit_set_vehicle_by_name(struct navit *n,const char *name)
2680 struct attr_iter *iter;
2681 struct attr vehicle_attr, name_attr;
2683 iter=navit_attr_iter_new();
2685 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2686 v=vehicle_attr.u.vehicle;
2687 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2688 if (name_attr.type==attr_name) {
2689 if (!strcmp(name,name_attr.u.str)) {
2690 navit_set_attr(n,&vehicle_attr);
2691 navit_attr_iter_destroy(iter);
2696 navit_attr_iter_destroy(iter);
2701 navit_set_layout_by_name(struct navit *n,const char *name)
2704 struct attr_iter iter;
2705 struct attr layout_attr;
2709 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2710 return 0; //No layouts - nothing to do
2712 if (iter.u.list==NULL) {
2716 iter.u.list=g_list_first(iter.u.list);
2718 while(iter.u.list) {
2719 l=(struct layout*)iter.u.list->data;
2720 if (!strcmp(name,l->name)) {
2721 layout_attr.u.layout=l;
2722 layout_attr.type=attr_layout;
2723 navit_set_attr(n,&layout_attr);
2724 iter.u.list=g_list_first(iter.u.list);
2727 iter.u.list=g_list_next(iter.u.list);
2730 iter.u.list=g_list_first(iter.u.list);
2735 navit_disable_suspend() {
2736 gui_disable_suspend(global_navit->gui);
2737 callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2741 navit_block(struct navit *this_, int block)
2744 this_->blocked |= 1;
2745 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2746 this_->blocked |= 2;
2749 if ((this_->blocked & 2) || block < 0) {
2759 navit_destroy(struct navit *this_)
2761 callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2763 /* TODO: destroy objects contained in this_ */
2765 vehicle_destroy(this_->vehicle->vehicle);
2766 if (this_->bookmarks) {
2767 char *center_file = bookmarks_get_center_file(TRUE);
2768 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2769 g_free(center_file);
2770 bookmarks_destroy(this_->bookmarks);
2772 callback_destroy(this_->nav_speech_cb);
2773 callback_destroy(this_->roadbook_callback);
2774 callback_destroy(this_->popup_callback);
2775 callback_destroy(this_->motion_timeout_callback);
2777 graphics_remove_callback(this_->gra, this_->resize_callback);
2778 callback_destroy(this_->resize_callback);
2780 graphics_remove_callback(this_->gra, this_->button_callback);
2781 callback_destroy(this_->button_callback);
2783 graphics_remove_callback(this_->gra, this_->motion_callback);
2784 callback_destroy(this_->motion_callback);
2786 graphics_remove_callback(this_->gra, this_->predraw_callback);
2787 callback_destroy(this_->predraw_callback);
2788 route_destroy(this_->route);
2789 graphics_free(this_->gra);