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 */
152 struct gui *main_loop_gui;
157 struct mapset_handle *mapset_handle;
161 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
162 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
163 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
164 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
165 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
166 static void navit_set_cursors(struct navit *this_);
167 static void navit_cmd_zoom_to_route(struct navit *this);
168 static void navit_cmd_set_center_cursor(struct navit *this_);
169 static void navit_cmd_announcer_toggle(struct navit *this_);
170 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
172 struct navit *global_navit;
175 navit_add_mapset(struct navit *this_, struct mapset *ms)
177 this_->mapsets = g_list_append(this_->mapsets, ms);
181 navit_get_mapset(struct navit *this_)
184 return this_->mapsets->data;
186 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
192 navit_get_tracking(struct navit *this_)
194 return this_->tracking;
198 * @brief Get the user data directory.
199 * @param[in] create - create the directory if it does not exist
201 * @return char * to the data directory string.
203 * returns the directory used to store user data files (center.txt,
204 * destination.txt, bookmark.txt, ...)
208 navit_get_user_data_directory(int create) {
210 dir = getenv("NAVIT_USER_DATADIR");
211 if (create && !file_exists(dir)) {
212 dbg(0,"creating dir %s\n", dir);
213 if (file_mkdir(dir,0)) {
214 dbg(0,"failed creating dir %s\n", dir);
219 } /* end: navit_get_user_data_directory(gboolean create) */
223 navit_draw_async(struct navit *this_, int async)
226 if (this_->blocked) {
230 transform_setup_source_rect(this_->trans);
231 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
235 navit_draw(struct navit *this_)
237 if (this_->ready == 3)
238 navit_draw_async(this_, 0);
242 navit_get_ready(struct navit *this_)
250 navit_draw_displaylist(struct navit *this_)
252 if (this_->ready == 3)
253 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
257 navit_map_progress(struct navit *this_)
261 struct mapset_handle *msh;
264 if (this_->ready != 3)
269 ms=this_->mapsets->data;
271 while (msh && (map=mapset_next(msh, 0))) {
272 if (map_get_attr(map, attr_progress, &attr, NULL)) {
273 char *str=g_strdup_printf("%s ",attr.u.str);
274 graphics_draw_mode(this_->gra, draw_mode_begin);
275 graphics_draw_text_std(this_->gra, 16, str, &p);
278 graphics_draw_mode(this_->gra, draw_mode_end);
285 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
288 if (attr->type != attr_route_status)
291 if (this_->ready != 3)
293 if (updated != route_status_path_done_new)
295 if (this_->vehicle) {
296 if (this_->vehicle->follow_curr == 1)
298 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
299 this_->vehicle->follow_curr=this_->vehicle->follow;
305 navit_handle_resize(struct navit *this_, int w, int h)
307 struct map_selection sel;
308 int callback=(this_->ready == 1);
310 memset(&sel, 0, sizeof(sel));
315 transform_set_screen_selection(this_->trans, &sel);
316 graphics_init(this_->gra);
317 graphics_set_rect(this_->gra, &sel.u.p_rect);
319 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
320 if (this_->ready == 3)
321 navit_draw_async(this_, 1);
325 navit_resize(void *data, int w, int h)
327 struct navit *this=data;
328 if (!this->ignore_graphics_events)
329 navit_handle_resize(this, w, h);
333 navit_get_width(struct navit *this_)
340 navit_get_height(struct navit *this_)
346 navit_popup(void *data)
348 struct navit *this_=data;
349 popup(this_, 1, &this_->pressed);
350 this_->button_timeout=NULL;
356 navit_ignore_button(struct navit *this_)
358 if (this_->ignore_button)
360 this_->ignore_button=1;
365 navit_ignore_graphics_events(struct navit *this_, int ignore)
367 this_->ignore_graphics_events=ignore;
371 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
376 double angleo,anglen;
378 if (!transform_reverse(tr, old, &co))
381 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
382 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
383 yaw=transform_get_yaw(tr)+angleo-anglen;
384 transform_set_yaw(tr, yaw % 360);
386 if (!transform_reverse(tr, new, &cn))
388 cp=transform_get_center(tr);
391 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
392 transform_set_center(tr, &c);
396 navit_set_timeout(struct navit *this_)
399 follow.type=attr_follow;
400 follow.u.num=this_->center_timeout;
401 navit_set_attr(this_, &follow);
405 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
409 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
410 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
411 if (this_->ignore_button) {
412 this_->ignore_button=0;
420 this_->button_pressed=1;
424 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
427 navit_set_center_screen(this_, p, 1);
429 popup(this_, button, p);
430 if (button == 4 && this_->use_mousewheel) {
432 navit_zoom_in(this_, 2, p);
434 if (button == 5 && this_->use_mousewheel) {
436 navit_zoom_out(this_, 2, p);
440 this_->button_pressed=0;
441 if (this_->button_timeout) {
442 event_remove_timeout(this_->button_timeout);
443 this_->button_timeout=NULL;
444 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
445 navit_set_center_screen(this_, p, !this_->zoomed);
448 if (this_->motion_timeout) {
449 event_remove_timeout(this_->motion_timeout);
450 this_->motion_timeout=NULL;
457 update_transformation(this_->trans, &this_->pressed, p, &pr);
459 update_transformation(this_->trans, &this_->pressed, p, NULL);
461 graphics_draw_drag(this_->gra, NULL);
462 transform_copy(this_->trans, this_->trans_cursor);
463 graphics_overlay_disable(this_->gra, 0);
465 navit_set_timeout(this_);
474 navit_button(void *data, int pressed, int button, struct point *p)
476 struct navit *this=data;
477 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
478 if (!this->ignore_graphics_events) {
479 if (! this->popup_callback)
480 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
481 navit_handle_button(this, pressed, button, p, this->popup_callback);
487 navit_motion_timeout(struct navit *this_)
491 if (this_->drag_bitmap) {
493 point.x=(this_->current.x-this_->pressed.x);
494 point.y=(this_->current.y-this_->pressed.y);
495 if (graphics_draw_drag(this_->gra, &point)) {
496 graphics_overlay_disable(this_->gra, 1);
497 graphics_draw_mode(this_->gra, draw_mode_end);
499 this_->motion_timeout=NULL;
503 dx=(this_->current.x-this_->last.x);
504 dy=(this_->current.y-this_->last.y);
506 struct transformation *tr;
508 this_->last=this_->current;
509 graphics_overlay_disable(this_->gra, 1);
510 tr=transform_dup(this_->trans);
514 update_transformation(tr, &this_->pressed, &this_->current, &pr);
516 update_transformation(tr, &this_->pressed, &this_->current, NULL);
519 graphics_displaylist_move(this_->displaylist, dx, dy);
521 graphics_draw_cancel(this_->gra, this_->displaylist);
522 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
523 transform_destroy(tr);
526 this_->motion_timeout=NULL;
531 navit_handle_motion(struct navit *this_, struct point *p)
535 if (this_->button_pressed && !this_->popped) {
536 dx=(p->x-this_->pressed.x);
537 dy=(p->y-this_->pressed.y);
538 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
540 if (this_->button_timeout) {
541 event_remove_timeout(this_->button_timeout);
542 this_->button_timeout=NULL;
545 if (! this_->motion_timeout_callback)
546 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
547 if (! this_->motion_timeout)
548 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
554 navit_motion(void *data, struct point *p)
556 struct navit *this=data;
557 if (!this->ignore_graphics_events)
558 navit_handle_motion(this, p);
562 navit_predraw(struct navit *this_)
565 struct navit_vehicle *nv;
566 transform_copy(this_->trans, this_->trans_cursor);
570 navit_vehicle_draw(this_, nv, NULL);
576 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
578 struct coord c1, c2, *center;
579 if (scale < this_->zoom_min)
580 scale=this_->zoom_min;
581 if (scale > this_->zoom_max)
582 scale=this_->zoom_max;
584 transform_reverse(this_->trans, p, &c1);
585 transform_set_scale(this_->trans, scale);
587 transform_reverse(this_->trans, p, &c2);
588 center = transform_center(this_->trans);
589 center->x += c1.x - c2.x;
590 center->y += c1.y - c2.y;
597 * @brief Automatically adjusts zoom level
599 * This function automatically adjusts the current
600 * zoom level according to the current speed.
602 * @param this_ The navit struct
603 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
604 * @param speed The vehicles speed in meters per second
605 * @param dir The direction into which the vehicle moves
608 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
615 if (! this_->autozoom_active) {
619 distance = speed * this_->autozoom_secs;
621 transform_get_size(this_->trans, &w, &h);
622 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
623 scale = transform_get_scale(this_->trans);
625 /* We make sure that the point we want to see is within a certain range
626 * around the vehicle. The radius of this circle is the size of the
627 * screen. This doesn't necessarily mean the point is visible because of
628 * perspective etc. Quite rough, but should be enough. */
631 new_scale = (double)distance / h * 16;
633 new_scale = (double)distance / w * 16;
636 if (abs(new_scale - scale) < 2) {
640 if (new_scale >= this_->autozoom_min) {
641 navit_scale(this_, (long)new_scale, &pc, 0);
643 if (scale != this_->autozoom_min) {
644 navit_scale(this_, this_->autozoom_min, &pc, 0);
650 * Change the current zoom level, zooming closer to the ground
652 * @param navit The navit instance
653 * @param factor The zoom factor, usually 2
654 * @param p The invariant point (if set to NULL, default to center)
658 navit_zoom_in(struct navit *this_, int factor, struct point *p)
660 long scale=transform_get_scale(this_->trans)/factor;
663 navit_scale(this_, scale, p, 1);
667 * Change the current zoom level
669 * @param navit The navit instance
670 * @param factor The zoom factor, usually 2
671 * @param p The invariant point (if set to NULL, default to center)
675 navit_zoom_out(struct navit *this_, int factor, struct point *p)
677 long scale=transform_get_scale(this_->trans)*factor;
678 navit_scale(this_, scale, p, 1);
682 navit_zoom_in_cursor(struct navit *this_, int factor)
685 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
686 navit_zoom_in(this_, factor, &p);
687 this_->vehicle->follow_curr=this_->vehicle->follow;
689 navit_zoom_in(this_, factor, NULL);
693 navit_zoom_out_cursor(struct navit *this_, int factor)
696 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
697 navit_zoom_out(this_, 2, &p);
698 this_->vehicle->follow_curr=this_->vehicle->follow;
700 navit_zoom_out(this_, 2, NULL);
704 navit_cmd_zoom_in(struct navit *this_)
706 navit_zoom_in_cursor(this_, 2);
711 navit_cmd_zoom_out(struct navit *this_)
713 navit_zoom_out_cursor(this_, 2);
719 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
721 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
722 navit_say(this, in[0]->u.str);
725 static GHashTable *cmd_int_var_hash = NULL;
728 * Store key value pair for the command system
730 * @param navit The navit instance
731 * @param function unused (needed to match command function signiture)
732 * @param in input attributes in[0] is the key string, in[1] is the integer value to store
733 * @param out output attributes, unused
734 * @param valid unused
738 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
740 if(!cmd_int_var_hash) {
741 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
744 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
745 (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
747 struct attr*val = g_new(struct attr,1);
748 attr_dup_content(in[1],val);
749 key = g_strdup(in[0]->u.str);
750 g_hash_table_insert(cmd_int_var_hash, key, val);
755 * Get value given a key string for the command system
757 * @param navit The navit instance
758 * @param function unused (needed to match command function signiture)
759 * @param in input attribute in[0] is the key string
760 * @param out output attribute, the value for the given key string if exists or 0
761 * @param valid unused
765 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
767 struct attr **list = g_new0(struct attr *,2);
768 if(!cmd_int_var_hash) {
769 struct attr*val = g_new0(struct attr,1);
770 val->type = attr_type_int_begin;
774 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
775 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
780 struct attr*val = g_new0(struct attr,1);
781 val->type = attr_type_int_begin;
790 GList *cmd_int_var_stack = NULL;
793 * Push an integer to the stack for the command system
795 * @param navit The navit instance
796 * @param function unused (needed to match command function signiture)
797 * @param in input attribute in[0] is the integer attibute to push
798 * @param out output attributes, unused
799 * @param valid unused
803 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
805 if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
806 struct attr*val = g_new(struct attr,1);
807 attr_dup_content(in[0],val);
808 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
813 * Pop an integer from the command system's integer stack
815 * @param navit The navit instance
816 * @param function unused (needed to match command function signiture)
817 * @param in input attributes unused
818 * @param out output attribute, the value popped if stack isn't empty or 0
819 * @param valid unused
823 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
825 struct attr **list = g_new0(struct attr *,2);
826 if(!cmd_int_var_stack) {
827 struct attr*val = g_new0(struct attr,1);
828 val->type = attr_type_int_begin;
833 list[0] = cmd_int_var_stack->data;
834 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
841 * Get current size of command system's integer stack
843 * @param navit The navit instance
844 * @param function unused (needed to match command function signiture)
845 * @param in input attributes unused
846 * @param out output attribute, the size of stack
847 * @param valid unused
851 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
854 struct attr *attr = g_new0(struct attr ,1);
855 attr->type = attr_type_int_begin;
856 if(!cmd_int_var_stack) {
860 attr->u.num = g_list_length(cmd_int_var_stack);
862 list = g_new0(struct attr *,2);
866 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
870 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
873 char *description=NULL;
878 pc.pro = transform_get_projection(this->trans);
879 if (ATTR_IS_COORD(in[0]->type)) {
880 pc.x=in[0]->u.coord->x;
881 pc.y=in[0]->u.coord->y;
883 } else if (ATTR_IS_PCOORD(in[0]->type)) {
886 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
891 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
897 if (in[0] && ATTR_IS_STRING(in[0]->type))
898 description=in[0]->u.str;
899 navit_set_destination(this, &pc, description, 1);
903 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
906 attr.type=attr_type_string_begin;
909 *out=attr_generic_add_attr(*out, &attr);
913 static struct command_table commands[] = {
914 {"zoom_in",command_cast(navit_cmd_zoom_in)},
915 {"zoom_out",command_cast(navit_cmd_zoom_out)},
916 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
917 {"say",command_cast(navit_cmd_say)},
918 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
919 {"set_destination",command_cast(navit_cmd_set_destination)},
920 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
921 {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
922 {"set_int_var",command_cast(navit_cmd_set_int_var)},
923 {"get_int_var",command_cast(navit_cmd_get_int_var)},
924 {"push_int",command_cast(navit_cmd_push_int)},
925 {"pop_int",command_cast(navit_cmd_pop_int)},
926 {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
930 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
932 command_add_table(this_->attr_cbl, commands, count, this_);
936 navit_new(struct attr *parent, struct attr **attrs)
938 struct navit *this_=g_new0(struct navit, 1);
939 struct pcoord center;
942 enum projection pro=projection_mg;
947 this_->self.type=attr_navit;
948 this_->self.u.navit=this_;
949 this_->attr_cbl=callback_list_new();
951 this_->orientation=-1;
952 this_->tracking_flag=1;
953 this_->recentdest_count=10;
954 this_->osd_configuration=-1;
956 this_->center_timeout = 10;
957 this_->use_mousewheel = 1;
958 this_->autozoom_secs = 10;
959 this_->autozoom_min = 7;
960 this_->autozoom_active = 0;
962 this_->zoom_max = 2097152;
963 this_->follow_cursor = 1;
967 this_->trans = transform_new();
968 this_->trans_cursor = transform_new();
969 transform_from_geo(pro, &g, &co);
974 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
976 this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
980 for (;*attrs; attrs++) {
981 navit_set_attr_do(this_, *attrs, 1);
983 this_->displaylist=graphics_displaylist_new();
984 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
986 this_->messages = messagelist_new(attrs);
992 navit_set_gui(struct navit *this_, struct gui *gui)
997 if (gui_has_main_loop(this_->gui)) {
998 if (! main_loop_gui) {
999 main_loop_gui=this_->gui;
1001 dbg(0,"gui with main loop already active, ignoring this instance");
1009 navit_add_message(struct navit *this_, char *message)
1011 message_new(this_->messages, message);
1015 *navit_get_messages(struct navit *this_)
1017 return message_get(this_->messages);
1021 navit_set_graphics(struct navit *this_, struct graphics *gra)
1026 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1027 graphics_add_callback(gra, this_->resize_callback);
1028 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1029 graphics_add_callback(gra, this_->button_callback);
1030 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1031 graphics_add_callback(gra, this_->motion_callback);
1032 this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1033 graphics_add_callback(gra, this_->predraw_callback);
1038 navit_get_graphics(struct navit *this_)
1043 struct vehicleprofile *
1044 navit_get_vehicleprofile(struct navit *this_)
1046 return this_->vehicleprofile;
1050 navit_get_vehicleprofiles(struct navit *this_)
1052 return this_->vehicleprofiles;
1056 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1061 c=transform_center(this_->trans);
1062 transform_to_geo(transform_get_projection(this_->trans), c, &g);
1063 transform_set_projection(this_->trans, pro);
1064 transform_from_geo(pro, &g, c);
1070 * Start the route computing to a given set of coordinates
1072 * @param navit The navit instance
1073 * @param c The coordinate to start routing to
1074 * @param description A label which allows the user to later identify this destination in the former destinations selection
1078 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1080 char *destination_file;
1082 this_->destination=*c;
1083 this_->destination_valid=1;
1085 dbg(1, "navit->navit_set_destination %i\n", c->x);
1086 dbg(1, "navit->navit_set_destination %i\n", c->y);
1089 this_->destination_valid=0;
1090 destination_file = bookmarks_get_destination_file(TRUE);
1091 bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
1092 g_free(destination_file);
1093 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1095 route_set_destination(this_->route, c, async);
1097 if (this_->ready == 3)
1103 * Start the route computing to a given set of coordinates including waypoints
1105 * @param navit The navit instance
1106 * @param c The coordinate to start routing to
1107 * @param description A label which allows the user to later identify this destination in the former destinations selection
1111 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1113 char *destination_file;
1115 this_->destination=c[count-1];
1116 this_->destination_valid=1;
1118 this_->destination_valid=0;
1119 destination_file = bookmarks_get_destination_file(TRUE);
1120 bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
1121 g_free(destination_file);
1122 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1124 route_set_destinations(this_->route, c, count, async);
1126 if (this_->ready == 3)
1132 * @brief Checks if a route is calculated
1134 * This function checks if a route is calculated.
1136 * @param this_ The navit struct whose route should be checked.
1137 * @return True if the route is set, false otherwise.
1140 navit_check_route(struct navit *this_)
1143 return route_get_path_set(this_->route);
1150 navit_former_destinations_active(struct navit *this_)
1152 char *destination_file = bookmarks_get_destination_file(FALSE);
1156 f=fopen(destination_file,"r");
1158 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
1162 g_free(destination_file);
1167 navit_add_former_destinations_from_file(struct navit *this_)
1169 char *destination_file = bookmarks_get_destination_file(FALSE);
1170 struct attr *attrs[4];
1171 struct map_rect *mr;
1173 int i,valid=0,count=0;
1175 struct pcoord pc[16];
1181 parent.type=attr_navit;
1182 parent.u.navit=this_;
1184 type.type=attr_type;
1185 type.u.str="textfile";
1187 data.type=attr_data;
1188 data.u.str=destination_file;
1190 flags.type=attr_flags;
1193 attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1195 this_->former_destination=map_new(&parent, attrs);
1196 g_free(destination_file);
1197 if (!this_->route || !navit_former_destinations_active(this_))
1199 mr=map_rect_new(this_->former_destination, NULL);
1200 while ((item=map_rect_get_item(mr))) {
1201 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
1204 map_rect_destroy(mr);
1205 if (valid && count > 0) {
1206 for (i = 0 ; i < count ; i++) {
1207 pc[i].pro=map_projection(this_->former_destination);
1212 route_set_destination(this_->route, &pc[0], 1);
1214 route_set_destinations(this_->route, pc, count, 1);
1215 this_->destination=pc[count-1];
1216 this_->destination_valid=1;
1222 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1227 if (this_->textfile_debug_log && this_->vehicle) {
1228 str1=g_strdup_vprintf(fmt, ap);
1229 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1230 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1238 navit_say(struct navit *this_, char *text)
1241 speech_say(this_->speech, text);
1246 * @brief Toggles the navigation announcer for navit
1247 * @param this_ The navit object
1250 navit_cmd_announcer_toggle(struct navit *this_)
1252 struct attr attr, speechattr;
1254 // search for the speech attribute
1255 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1257 // find out if the corresponding attribute attr_active has been set
1258 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1260 attr.u.num = !attr.u.num;
1262 // otherwise disable it because voice is enabled by default
1263 attr.type = attr_active;
1267 // apply the new state
1268 if(!speech_set_attr(speechattr.u.speech, &attr))
1271 // announce that the speech attribute has changed
1272 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1276 navit_speak(struct navit *this_)
1278 struct navigation *nav=this_->navigation;
1279 struct map *map=NULL;
1280 struct map_rect *mr=NULL;
1284 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1286 dbg(1, "this_.speech->active %i\n", attr.u.num);
1291 map=navigation_get_map(nav);
1293 mr=map_rect_new(map, NULL);
1295 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1296 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1297 speech_say(this_->speech, attr.u.str);
1298 navit_add_message(this_, attr.u.str);
1299 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1301 map_rect_destroy(mr);
1306 navit_window_roadbook_update(struct navit *this_)
1308 struct navigation *nav=this_->navigation;
1309 struct map *map=NULL;
1310 struct map_rect *mr=NULL;
1313 struct param_list param[5];
1317 datawindow_mode(this_->roadbook_window, 1);
1319 map=navigation_get_map(nav);
1321 mr=map_rect_new(map, NULL);
1322 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1324 dbg(0,"while loop\n");
1325 while ((item=map_rect_get_item(mr))) {
1326 dbg(0,"item=%p\n", item);
1328 if (item->type != type_nav_position) {
1329 item_attr_get(item, attr_navigation_long, &attr);
1330 if (attr.u.str == NULL) {
1333 dbg(2, "Command='%s'\n", attr.u.str);
1334 param[0].value=g_strdup(attr.u.str);
1336 param[0].value=_("Position");
1337 param[0].name=_("Command");
1339 item_attr_get(item, attr_length, &attr);
1340 dbg(2, "Length=%d\n", attr.u.num);
1341 param[1].name=_("Length");
1343 if ( attr.u.num >= 2000 )
1345 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1349 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1352 item_attr_get(item, attr_time, &attr);
1353 dbg(2, "Time=%d\n", attr.u.num);
1355 param[2].name=_("Time");
1358 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1362 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1365 item_attr_get(item, attr_destination_length, &attr);
1366 dbg(2, "Destlength=%d\n", attr.u.num);
1367 param[3].name=_("Destination Length");
1368 if ( attr.u.num >= 2000 )
1370 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1374 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1377 item_attr_get(item, attr_destination_time, &attr);
1378 dbg(2, "Desttime=%d\n", attr.u.num);
1380 param[4].name=_("Destination Time");
1383 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1387 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1389 datawindow_add(this_->roadbook_window, param, 5);
1391 map_rect_destroy(mr);
1393 datawindow_mode(this_->roadbook_window, 0);
1397 navit_window_roadbook_destroy(struct navit *this_)
1400 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1401 this_->roadbook_window=NULL;
1402 this_->roadbook_callback=NULL;
1405 navit_window_roadbook_new(struct navit *this_)
1407 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1411 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1412 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1413 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1414 navit_window_roadbook_update(this_);
1418 navit_init(struct navit *this_)
1425 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1427 if (!this_->gui && !(this_->flags & 2)) {
1429 navit_destroy(this_);
1432 if (!this_->gra && !(this_->flags & 1)) {
1433 dbg(0,"no graphics\n");
1434 navit_destroy(this_);
1437 dbg(2,"Connecting gui to graphics\n");
1438 if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1439 struct attr attr_type_gui, attr_type_graphics;
1440 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1441 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1442 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1443 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1444 dbg(0," for explanations and solutions\n");
1446 navit_destroy(this_);
1449 if (this_->speech && this_->navigation) {
1451 speech.type=attr_speech;
1452 speech.u.speech=this_->speech;
1453 navigation_set_attr(this_->navigation, &speech);
1455 dbg(2,"Initializing graphics\n");
1456 dbg(2,"Setting Vehicle\n");
1457 navit_set_vehicle(this_, this_->vehicle);
1458 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1459 if (this_->mapsets) {
1460 struct mapset_handle *msh;
1461 ms=this_->mapsets->data;
1462 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1463 msh=mapset_open(ms);
1464 while (msh && (map=mapset_next(msh, 0))) {
1465 map_add_callback(map, this_->progress_cb);
1470 if ((map=route_get_map(this_->route))) {
1472 map_a.type=attr_map;
1474 mapset_add_attr(ms, &map_a);
1476 if ((map=route_get_graph_map(this_->route))) {
1477 struct attr map_a,active;
1478 map_a.type=attr_map;
1480 active.type=attr_active;
1482 mapset_add_attr(ms, &map_a);
1483 map_set_attr(map, &active);
1485 route_set_mapset(this_->route, ms);
1486 route_set_projection(this_->route, transform_get_projection(this_->trans));
1488 if (this_->tracking) {
1489 tracking_set_mapset(this_->tracking, ms);
1491 tracking_set_route(this_->tracking, this_->route);
1493 if (this_->navigation) {
1494 if ((map=navigation_get_map(this_->navigation))) {
1495 struct attr map_a,active;
1496 map_a.type=attr_map;
1498 active.type=attr_active;
1500 mapset_add_attr(ms, &map_a);
1501 map_set_attr(map, &active);
1504 if (this_->tracking) {
1505 if ((map=tracking_get_map(this_->tracking))) {
1506 struct attr map_a,active;
1507 map_a.type=attr_map;
1509 active.type=attr_active;
1511 mapset_add_attr(ms, &map_a);
1512 map_set_attr(map, &active);
1515 navit_add_former_destinations_from_file(this_);
1518 struct attr callback;
1519 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1520 callback.type=attr_callback;
1521 callback.u.callback=this_->route_cb;
1522 route_add_attr(this_->route, &callback);
1524 if (this_->navigation) {
1525 if (this_->speech) {
1526 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1527 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1530 navigation_set_route(this_->navigation, this_->route);
1532 dbg(2,"Setting Center\n");
1533 center_file = bookmarks_get_center_file(FALSE);
1534 bookmarks_set_center_from_file(this_->bookmarks, center_file);
1535 g_free(center_file);
1537 if (this_->menubar) {
1538 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1540 navit_add_menu_windows_items(this_, men);
1546 navit_window_roadbook_new(this_);
1547 navit_window_items_new(this_);
1550 messagelist_init(this_->messages);
1552 navit_set_cursors(this_);
1554 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1555 callback=(this_->ready == 2);
1557 dbg(2,"ready=%d\n",this_->ready);
1558 if (this_->ready == 3)
1559 navit_draw_async(this_, 1);
1561 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1563 routech_test(this_);
1568 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1573 c.x=(r->rl.x+r->lu.x)/2;
1574 c.y=(r->rl.y+r->lu.y)/2;
1575 transform_set_center(this_->trans, &c);
1576 dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1577 while (scale < 1<<20) {
1579 transform_set_scale(this_->trans, scale);
1580 transform_setup_source_rect(this_->trans);
1581 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1582 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1583 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1584 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1585 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1591 if (this_->ready == 3)
1592 navit_draw_async(this_,0);
1596 navit_zoom_to_route(struct navit *this_, int orientation)
1599 struct map_rect *mr=NULL;
1602 struct coord_rect r;
1607 map=route_get_map(this_->route);
1608 dbg(1,"map=%p\n",map);
1610 mr=map_rect_new(map, NULL);
1611 dbg(1,"mr=%p\n",mr);
1613 while ((item=map_rect_get_item(mr))) {
1614 dbg(1,"item=%s\n", item_to_name(item->type));
1615 while (item_coord_get(item, &c, 1)) {
1620 coord_rect_extend(&r, &c);
1627 if (orientation != -1)
1628 transform_set_yaw(this_->trans, orientation);
1629 navit_zoom_to_rect(this_, &r);
1633 navit_cmd_zoom_to_route(struct navit *this)
1635 navit_zoom_to_route(this, 0);
1640 * Change the current zoom level
1642 * @param navit The navit instance
1643 * @param center The point where to center the map, including its projection
1647 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1649 struct coord *c=transform_center(this_->trans);
1651 enum projection pro = transform_get_projection(this_->trans);
1652 if (pro != center->pro) {
1655 transform_from_to(&c1, center->pro, &c2, pro);
1662 navit_set_timeout(this_);
1663 if (this_->ready == 3)
1668 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1672 transform_set_center(this_->trans, c);
1673 transform_get_size(this_->trans, &width, &height);
1676 update_transformation(this_->trans, &po, p, NULL);
1678 navit_set_timeout(this_);
1682 * Links all vehicles to a cursor depending on the current profile.
1684 * @param this_ A navit instance
1685 * @author Ralph Sennhauser (10/2009)
1688 navit_set_cursors(struct navit *this_)
1691 struct navit_vehicle *nv;
1695 v=g_list_first(this_->vehicles); // GList of navit_vehicles
1698 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1699 if (!strcmp(name.u.str,"none"))
1702 c=layout_get_cursor(this_->layout_current, name.u.str);
1704 c=layout_get_cursor(this_->layout_current, "default");
1705 vehicle_set_cursor(nv->vehicle, c);
1712 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1715 struct navit_vehicle *nv=this_->vehicle;
1717 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
1718 #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 */
1719 float min_offset = 0.; // Percent offset at min_offset_speed.
1720 float max_offset = 30.; // Percent offset at max_offset_speed.
1721 int min_offset_speed = 2; // Speed in km/h
1722 int max_offset_speed = 50; // Speed ini km/h
1723 // Calculate cursor offset from the center of the screen, upon speed.
1724 if (nv->speed <= min_offset_speed) {
1725 offset = min_offset;
1726 } else if (nv->speed > max_offset_speed) {
1727 offset = max_offset;
1729 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1733 transform_get_size(this_->trans, &width, &height);
1734 if (this_->orientation == -1 || keep_orientation) {
1736 p->y=(50 + offset)*height/100;
1738 *dir=keep_orientation?this_->orientation:nv->dir;
1741 if (this_->tracking && this_->tracking_flag) {
1742 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1744 mdir=nv->dir-this_->orientation;
1747 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1748 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1750 *dir=this_->orientation;
1756 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
1760 struct navit_vehicle *nv=this_->vehicle;
1761 navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
1762 transform_set_yaw(this_->trans, dir);
1763 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1765 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1769 navit_set_center_cursor_draw(struct navit *this_)
1771 navit_set_center_cursor(this_,1,0);
1772 if (this_->ready == 3)
1773 navit_draw_async(this_, 1);
1777 navit_cmd_set_center_cursor(struct navit *this_)
1779 navit_set_center_cursor_draw(this_);
1783 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1787 transform_reverse(this_->trans, p, &c);
1790 pc.pro = transform_get_projection(this_->trans);
1791 navit_set_center(this_, &pc, set_timeout);
1795 switch((*attrs)->type) {
1797 zoom=(*attrs)->u.num;
1800 g=*((*attrs)->u.coord_geo);
1805 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1807 int dir=0, orient_old=0, attr_updated=0;
1811 struct navit_vehicle *nv;
1814 active.type=attr_active;
1817 switch (attr->type) {
1819 attr_updated=(this_->autozoom_secs != attr->u.num);
1820 this_->autozoom_secs = attr->u.num;
1822 case attr_autozoom_active:
1823 attr_updated=(this_->autozoom_active != attr->u.num);
1824 this_->autozoom_active = attr->u.num;
1827 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1828 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1829 transform_set_center(this_->trans, &co);
1831 case attr_drag_bitmap:
1832 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1833 this_->drag_bitmap=!!attr->u.num;
1836 attr_updated=(this_->flags != attr->u.num);
1837 this_->flags=attr->u.num;
1839 case attr_flags_graphics:
1840 attr_updated=(this_->graphics_flags != attr->u.num);
1841 this_->graphics_flags=attr->u.num;
1844 if (!this_->vehicle)
1846 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1847 this_->vehicle->follow_curr = attr->u.num;
1850 if(this_->layout_current!=attr->u.layout) {
1851 this_->layout_current=attr->u.layout;
1852 graphics_font_destroy_all(this_->gra);
1853 navit_set_cursors(this_);
1854 if (this_->ready == 3)
1859 case attr_layout_name:
1863 if (!strcmp(lay->name,attr->u.str)) {
1865 attr.type=attr_layout;
1867 return navit_set_attr_do(this_, &attr, init);
1872 case attr_map_border:
1873 if (this_->border != attr->u.num) {
1874 this_->border=attr->u.num;
1878 case attr_orientation:
1879 orient_old=this_->orientation;
1880 this_->orientation=attr->u.num;
1882 if (this_->orientation != -1) {
1883 dir = this_->orientation;
1885 if (this_->vehicle) {
1886 dir = this_->vehicle->dir;
1889 transform_set_yaw(this_->trans, dir);
1890 if (orient_old != this_->orientation) {
1892 if (this_->ready == 3)
1899 case attr_osd_configuration:
1900 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1901 attr_updated=(this_->osd_configuration != attr->u.num);
1902 this_->osd_configuration=attr->u.num;
1905 attr_updated=(this_->pitch != attr->u.num);
1906 this_->pitch=attr->u.num;
1907 transform_set_pitch(this_->trans, this_->pitch);
1908 if (!init && attr_updated && this_->ready == 3)
1911 case attr_projection:
1912 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1913 navit_projection_set(this_, attr->u.projection, !init);
1918 attr_updated=(this_->radius != attr->u.num);
1919 this_->radius=attr->u.num;
1921 case attr_recent_dest:
1922 attr_updated=(this_->recentdest_count != attr->u.num);
1923 this_->recentdest_count=attr->u.num;
1926 if(this_->speech && this_->speech != attr->u.speech) {
1928 this_->speech = attr->u.speech;
1932 attr_updated=(this_->center_timeout != attr->u.num);
1933 this_->center_timeout = attr->u.num;
1936 attr_updated=(this_->tracking_flag != !!attr->u.num);
1937 this_->tracking_flag=!!attr->u.num;
1939 case attr_transformation:
1940 this_->trans=attr->u.transformation;
1942 case attr_use_mousewheel:
1943 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1944 this_->use_mousewheel=!!attr->u.num;
1950 if (nv->vehicle == attr->u.vehicle) {
1951 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1953 vehicle_set_attr(this_->vehicle->vehicle, &active);
1955 vehicle_set_attr(nv->vehicle, &active);
1958 navit_set_vehicle(this_, nv);
1964 zoom=transform_get_scale(this_->trans);
1965 attr_updated=(zoom != attr->u.num);
1966 transform_set_scale(this_->trans, attr->u.num);
1967 if (attr_updated && !init)
1971 attr_updated=(attr->u.num != this_->zoom_min);
1972 this_->zoom_min=attr->u.num;
1975 attr_updated=(attr->u.num != this_->zoom_max);
1976 this_->zoom_max=attr->u.num;
1979 navit_add_message(this_, attr->u.str);
1981 case attr_follow_cursor:
1982 attr_updated=(this_->follow_cursor != !!attr->u.num);
1983 this_->follow_cursor=!!attr->u.num;
1988 if (attr_updated && !init) {
1989 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1990 if (attr->type == attr_osd_configuration)
1991 graphics_draw_mode(this_->gra, draw_mode_end);
1997 navit_set_attr(struct navit *this_, struct attr *attr)
1999 return navit_set_attr_do(this_, attr, 0);
2003 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2005 struct message *msg;
2011 msg = navit_get_messages(this_);
2019 len += strlen(msg->text) + 1;
2022 attr->u.str = g_malloc(len + 1);
2024 msg = navit_get_messages(this_);
2027 g_stpcpy((attr->u.str + offset), msg->text);
2028 offset += strlen(msg->text);
2029 attr->u.str[offset] = '\n';
2035 attr->u.str[len] = '\0';
2037 case attr_bookmark_map:
2038 attr->u.map=bookmarks_get_map(this_->bookmarks);
2040 case attr_bookmarks:
2041 attr->u.bookmarks=this_->bookmarks;
2043 case attr_callback_list:
2044 attr->u.callback_list=this_->attr_cbl;
2046 case attr_destination:
2047 if (! this_->destination_valid)
2049 attr->u.pcoord=&this_->destination;
2051 case attr_displaylist:
2052 attr->u.displaylist=this_->displaylist;
2053 return (attr->u.displaylist != NULL);
2055 if (!this_->vehicle)
2057 attr->u.num=this_->vehicle->follow_curr;
2059 case attr_former_destination_map:
2060 attr->u.map=this_->former_destination;
2063 attr->u.graphics=this_->gra;
2064 ret=(attr->u.graphics != NULL);
2067 attr->u.gui=this_->gui;
2068 ret=(attr->u.gui != NULL);
2073 iter->u.list=g_list_next(iter->u.list);
2075 iter->u.list=this_->layouts;
2079 attr->u.layout=(struct layout *)iter->u.list->data;
2081 attr->u.layout=this_->layout_current;
2085 if (iter && this_->mapsets) {
2086 if (!iter->u.mapset_handle) {
2087 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2089 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2091 mapset_close(iter->u.mapset_handle);
2099 attr->u.mapset=this_->mapsets->data;
2100 ret=(attr->u.mapset != NULL);
2102 case attr_navigation:
2103 attr->u.navigation=this_->navigation;
2105 case attr_orientation:
2106 attr->u.num=this_->orientation;
2108 case attr_osd_configuration:
2109 attr->u.num=this_->osd_configuration;
2112 attr->u.num=transform_get_pitch(this_->trans);
2114 case attr_projection:
2116 attr->u.num=transform_get_projection(this_->trans);
2122 attr->u.route=this_->route;
2125 attr->u.speech=this_->speech;
2128 attr->u.num=this_->tracking_flag;
2130 case attr_trackingo:
2131 attr->u.tracking=this_->tracking;
2133 case attr_transformation:
2134 attr->u.transformation=this_->trans;
2139 iter->u.list=g_list_next(iter->u.list);
2141 iter->u.list=this_->vehicles;
2145 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2147 if(this_->vehicle) {
2148 attr->u.vehicle=this_->vehicle->vehicle;
2154 case attr_vehicleprofile:
2155 attr->u.vehicleprofile=this_->vehicleprofile;
2158 attr->u.num=transform_get_scale(this_->trans);
2160 case attr_autozoom_active:
2161 attr->u.num=this_->autozoom_active;
2163 case attr_follow_cursor:
2164 attr->u.num=this_->follow_cursor;
2174 navit_add_log(struct navit *this_, struct log *log)
2176 struct attr type_attr;
2177 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2179 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2180 char *header = "type=track_tracked\n";
2181 if (this_->textfile_debug_log)
2183 log_set_header(log, header, strlen(header));
2184 this_->textfile_debug_log=log;
2191 navit_add_attr(struct navit *this_, struct attr *attr)
2194 switch (attr->type) {
2196 navit_add_callback(this_, attr->u.callback);
2199 ret=navit_add_log(this_, attr->u.log);
2202 ret=navit_set_gui(this_, attr->u.gui);
2205 ret=navit_set_graphics(this_, attr->u.graphics);
2208 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2209 if(!this_->layout_current)
2210 this_->layout_current=attr->u.layout;
2213 this_->route=attr->u.route;
2216 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2218 case attr_navigation:
2219 this_->navigation=attr->u.navigation;
2221 case attr_recent_dest:
2222 this_->recentdest_count = attr->u.num;
2225 this_->speech=attr->u.speech;
2228 this_->tracking=attr->u.tracking;
2231 ret=navit_add_vehicle(this_, attr->u.vehicle);
2233 case attr_vehicleprofile:
2234 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2236 case attr_autozoom_min:
2237 this_->autozoom_min = attr->u.num;
2242 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2247 navit_remove_attr(struct navit *this_, struct attr *attr)
2250 switch (attr->type) {
2252 navit_remove_callback(this_, attr->u.callback);
2261 navit_attr_iter_new(void)
2263 return g_new0(struct attr_iter, 1);
2267 navit_attr_iter_destroy(struct attr_iter *iter)
2273 navit_add_callback(struct navit *this_, struct callback *cb)
2275 callback_list_add(this_->attr_cbl, cb);
2279 navit_remove_callback(struct navit *this_, struct callback *cb)
2281 callback_list_remove(this_->attr_cbl, cb);
2285 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2287 * @param navit The navit instance
2292 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2294 struct point cursor_pnt;
2295 enum projection pro;
2302 pro=transform_get_projection(this_->trans_cursor);
2305 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2307 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2312 pro=transform_get_projection(this_->trans);
2313 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2316 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2318 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2324 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2326 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2327 struct pcoord cursor_pc;
2328 struct point cursor_pnt, *pnt=&cursor_pnt;
2329 struct tracking *tracking=NULL;
2330 struct pcoord pc[16];
2331 enum projection pro=transform_get_projection(this_->trans_cursor);
2333 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2335 char *destination_file;
2338 if (this_->ready != 3) {
2339 profile(0,"return 1\n");
2342 navit_layout_switch(this_);
2343 if (this_->vehicle == nv && this_->tracking_flag)
2344 tracking=this_->tracking;
2346 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2347 attr_object=tracking;
2348 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2350 attr_object=nv->vehicle;
2351 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2353 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2354 if (!attr_valid.u.num != attr_position_valid_invalid)
2356 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2357 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2358 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2359 profile(0,"return 2\n");
2362 nv->dir=*attr_dir.u.numd;
2363 nv->speed=*attr_speed.u.numd;
2364 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2365 if (nv != this_->vehicle) {
2366 navit_vehicle_draw(this_, nv, NULL);
2367 profile(0,"return 3\n");
2370 cursor_pc.x = nv->coord.x;
2371 cursor_pc.y = nv->coord.y;
2372 cursor_pc.pro = pro;
2375 route_set_position_from_tracking(this_->route, tracking, pro);
2377 route_set_position(this_->route, &cursor_pc);
2379 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2380 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2381 if (this_->gui && nv->speed > 2)
2382 navit_disable_suspend();
2384 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2385 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2386 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2387 navit_set_center_cursor_draw(this_);
2389 navit_vehicle_draw(this_, nv, pnt);
2391 if (nv->follow_curr > 1)
2394 nv->follow_curr=nv->follow;
2395 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2397 /* Finally, if we reached our destination, stop navigation. */
2399 switch(route_destination_reached(this_->route)) {
2401 route_remove_waypoint(this_->route);
2402 count=route_get_destinations(this_->route, pc, 16);
2403 destination_file = bookmarks_get_destination_file(TRUE);
2404 bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2407 navit_set_destination(this_, NULL, NULL, 0);
2411 profile(0,"return 5\n");
2415 * Set the position of the vehicle
2417 * @param navit The navit instance
2418 * @param c The coordinate to set as position
2423 navit_set_position(struct navit *this_, struct pcoord *c)
2426 route_set_position(this_->route, c);
2427 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2429 if (this_->ready == 3)
2434 navit_set_vehicleprofile(struct navit *this_, char *name)
2438 l=this_->vehicleprofiles;
2440 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2441 if (!strcmp(attr.u.str, name)) {
2442 this_->vehicleprofile=l->data;
2444 route_set_profile(this_->route, this_->vehicleprofile);
2454 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2458 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2459 if (navit_set_vehicleprofile(this_, attr.u.str))
2462 if (!navit_set_vehicleprofile(this_,"car")) {
2463 /* We do not have a fallback "car" profile
2464 * so lets set any profile */
2466 l=this_->vehicleprofiles;
2468 this_->vehicleprofile=l->data;
2470 route_set_profile(this_->route, this_->vehicleprofile);
2476 * Register a new vehicle
2478 * @param navit The navit instance
2479 * @param v The vehicle instance
2480 * @returns 1 for success
2483 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2485 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2486 struct attr follow, active, animate;
2491 nv->animate_cursor=0;
2492 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2493 nv->follow=follow.u.num;
2494 nv->follow_curr=nv->follow;
2495 this_->vehicles=g_list_append(this_->vehicles, nv);
2496 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2497 navit_set_vehicle(this_, nv);
2498 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2499 nv->animate_cursor=animate.u.num;
2500 nv->callback.type=attr_callback;
2501 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2502 vehicle_add_attr(nv->vehicle, &nv->callback);
2503 vehicle_set_attr(nv->vehicle, &this_->self);
2511 navit_get_gui(struct navit *this_)
2516 struct transformation *
2517 navit_get_trans(struct navit *this_)
2519 return this_->trans;
2523 navit_get_route(struct navit *this_)
2525 return this_->route;
2529 navit_get_navigation(struct navit *this_)
2531 return this_->navigation;
2534 struct displaylist *
2535 navit_get_displaylist(struct navit *this_)
2537 return this_->displaylist;
2541 navit_layout_switch(struct navit *n)
2545 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2546 double trise,tset,trise_actual;
2548 int year, month, day;
2550 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2551 return; //No layout - nothing to switch
2555 l=layout_attr.u.layout;
2557 if (l->dayname || l->nightname) {
2558 //Ok, we know that we have profile to switch
2560 //Check that we aren't calculating too fast
2561 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2562 currTs=iso8601_to_secs(iso8601_attr.u.str);
2563 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2565 if (currTs-(n->prevTs)<60) {
2566 //We've have to wait a little
2569 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2571 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2572 return; //No valid fix yet
2574 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2575 //No position - no sun
2579 //We calculate sunrise anyway, cause it is needed both for day and for night
2580 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2581 //near the pole sun never rises/sets, so we should never switch profiles
2582 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2588 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2591 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
2592 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2593 //The sun is rising now!
2594 if (strcmp(l->name,l->dayname)) {
2595 navit_set_layout_by_name(n,l->dayname);
2600 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2601 //near the pole sun never rises/sets, so we should never switch profiles
2602 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2606 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2607 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2608 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
2609 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2611 if (strcmp(l->name,l->nightname)) {
2612 navit_set_layout_by_name(n,l->nightname);
2622 navit_set_vehicle_by_name(struct navit *n,const char *name)
2625 struct attr_iter *iter;
2626 struct attr vehicle_attr, name_attr;
2628 iter=navit_attr_iter_new();
2630 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2631 v=vehicle_attr.u.vehicle;
2632 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2633 if (name_attr.type==attr_name) {
2634 if (!strcmp(name,name_attr.u.str)) {
2635 navit_set_attr(n,&vehicle_attr);
2636 navit_attr_iter_destroy(iter);
2641 navit_attr_iter_destroy(iter);
2646 navit_set_layout_by_name(struct navit *n,const char *name)
2649 struct attr_iter iter;
2650 struct attr layout_attr;
2654 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2655 return 0; //No layouts - nothing to do
2657 if (iter.u.list==NULL) {
2661 iter.u.list=g_list_first(iter.u.list);
2663 while(iter.u.list) {
2664 l=(struct layout*)iter.u.list->data;
2665 if (!strcmp(name,l->name)) {
2666 layout_attr.u.layout=l;
2667 layout_attr.type=attr_layout;
2668 navit_set_attr(n,&layout_attr);
2669 iter.u.list=g_list_first(iter.u.list);
2672 iter.u.list=g_list_next(iter.u.list);
2675 iter.u.list=g_list_first(iter.u.list);
2680 navit_disable_suspend() {
2681 gui_disable_suspend(global_navit->gui);
2682 callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2686 navit_block(struct navit *this_, int block)
2689 this_->blocked |= 1;
2690 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2691 this_->blocked |= 2;
2694 if ((this_->blocked & 2) || block < 0) {
2704 navit_destroy(struct navit *this_)
2706 callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2708 /* TODO: destroy objects contained in this_ */
2710 vehicle_destroy(this_->vehicle->vehicle);
2711 if (this_->bookmarks) {
2712 char *center_file = bookmarks_get_center_file(TRUE);
2713 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2714 g_free(center_file);
2715 bookmarks_destroy(this_->bookmarks);
2717 callback_destroy(this_->nav_speech_cb);
2718 callback_destroy(this_->roadbook_callback);
2719 callback_destroy(this_->popup_callback);
2720 callback_destroy(this_->motion_timeout_callback);
2722 graphics_remove_callback(this_->gra, this_->resize_callback);
2723 callback_destroy(this_->resize_callback);
2725 graphics_remove_callback(this_->gra, this_->button_callback);
2726 callback_destroy(this_->button_callback);
2728 graphics_remove_callback(this_->gra, this_->motion_callback);
2729 callback_destroy(this_->motion_callback);
2731 graphics_remove_callback(this_->gra, this_->predraw_callback);
2732 callback_destroy(this_->predraw_callback);
2733 route_destroy(this_->route);