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.
35 #include "projection.h"
41 #include "transform.h"
46 #include "data_window.h"
48 #include "navigation.h"
59 #include "navit_nls.h"
63 #include "vehicleprofile.h"
65 #include "bookmarks.h"
68 * @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
72 //! The navit_vehicule
73 struct navit_vehicle {
75 /*! Limit of the follow counter. See navit_add_vehicle */
77 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
81 struct coord last; /*< Position of the last update of this vehicle */
82 struct vehicle *vehicle;
92 struct layout *layout_current;
94 struct action *action;
95 struct transformation *trans, *trans_cursor;
96 struct compass *compass;
98 struct navigation *navigation;
99 struct speech *speech;
100 struct tracking *tracking;
103 struct displaylist *displaylist;
106 int recentdest_count;
107 int osd_configuration;
109 GList *windows_items;
110 struct navit_vehicle *vehicle;
111 struct callback_list *attr_cbl;
112 struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb, *progress_cb;
113 struct datawindow *roadbook_window;
114 struct map *former_destination;
115 struct point pressed, last, current;
116 int button_pressed,moved,popped,zoomed;
121 struct event_timeout *button_timeout, *motion_timeout;
122 struct callback *motion_timeout_callback;
124 int ignore_graphics_events;
125 struct log *textfile_debug_log;
126 struct pcoord destination;
127 int destination_valid;
132 struct messagelist *messages;
133 struct callback *resize_callback,*button_callback,*motion_callback,*predraw_callback;
134 struct vehicleprofile *vehicleprofile;
135 GList *vehicleprofiles;
140 int zoom_min, zoom_max;
142 struct bookmarks *bookmarks;
144 /* 1=No graphics ok */
149 struct gui *main_loop_gui;
154 struct mapset_handle *mapset_handle;
158 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
159 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
160 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
161 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
162 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
163 static void navit_set_cursors(struct navit *this_);
164 static void navit_cmd_zoom_to_route(struct navit *this);
165 static void navit_cmd_set_center_cursor(struct navit *this_);
166 static void navit_cmd_announcer_toggle(struct navit *this_);
167 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
169 struct navit *global_navit;
172 navit_add_mapset(struct navit *this_, struct mapset *ms)
174 this_->mapsets = g_list_append(this_->mapsets, ms);
178 navit_get_mapset(struct navit *this_)
181 return this_->mapsets->data;
183 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
189 navit_get_tracking(struct navit *this_)
191 return this_->tracking;
195 * @brief Get the user data directory.
196 * @param[in] create - create the directory if it does not exist
198 * @return char * to the data directory string.
200 * returns the directory used to store user data files (center.txt,
201 * destination.txt, bookmark.txt, ...)
205 navit_get_user_data_directory(int create) {
207 dir = getenv("NAVIT_USER_DATADIR");
208 if (create && !file_exists(dir)) {
209 dbg(0,"creating dir %s\n", dir);
210 if (file_mkdir(dir,0)) {
211 dbg(0,"failed creating dir %s\n", dir);
216 } /* end: navit_get_user_data_directory(gboolean create) */
220 navit_draw_async(struct navit *this_, int async)
223 if (this_->blocked) {
227 transform_setup_source_rect(this_->trans);
228 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
232 navit_draw(struct navit *this_)
234 if (this_->ready == 3)
235 navit_draw_async(this_, 0);
239 navit_get_ready(struct navit *this_)
247 navit_draw_displaylist(struct navit *this_)
249 if (this_->ready == 3)
250 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
254 navit_map_progress(struct navit *this_)
258 struct mapset_handle *msh;
261 if (this_->ready != 3)
266 ms=this_->mapsets->data;
268 while (msh && (map=mapset_next(msh, 0))) {
269 if (map_get_attr(map, attr_progress, &attr, NULL)) {
270 char *str=g_strdup_printf("%s ",attr.u.str);
271 graphics_draw_mode(this_->gra, draw_mode_begin);
272 graphics_draw_text_std(this_->gra, 16, str, &p);
275 graphics_draw_mode(this_->gra, draw_mode_end);
282 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
285 if (attr->type != attr_route_status)
288 if (this_->ready != 3)
290 if (updated != route_status_path_done_new)
292 if (this_->vehicle) {
293 if (this_->vehicle->follow_curr == 1)
295 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
296 this_->vehicle->follow_curr=this_->vehicle->follow;
302 navit_handle_resize(struct navit *this_, int w, int h)
304 struct map_selection sel;
305 int callback=(this_->ready == 1);
307 memset(&sel, 0, sizeof(sel));
312 transform_set_screen_selection(this_->trans, &sel);
313 graphics_init(this_->gra);
314 graphics_set_rect(this_->gra, &sel.u.p_rect);
316 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
317 if (this_->ready == 3)
318 navit_draw_async(this_, 1);
322 navit_resize(void *data, int w, int h)
324 struct navit *this=data;
325 if (!this->ignore_graphics_events)
326 navit_handle_resize(this, w, h);
330 navit_get_width(struct navit *this_)
337 navit_get_height(struct navit *this_)
343 navit_popup(void *data)
345 struct navit *this_=data;
346 popup(this_, 1, &this_->pressed);
347 this_->button_timeout=NULL;
353 navit_ignore_button(struct navit *this_)
355 if (this_->ignore_button)
357 this_->ignore_button=1;
362 navit_ignore_graphics_events(struct navit *this_, int ignore)
364 this_->ignore_graphics_events=ignore;
368 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
373 double angleo,anglen;
375 if (!transform_reverse(tr, old, &co))
378 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
379 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
380 yaw=transform_get_yaw(tr)+angleo-anglen;
381 transform_set_yaw(tr, yaw % 360);
383 if (!transform_reverse(tr, new, &cn))
385 cp=transform_get_center(tr);
388 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
389 transform_set_center(tr, &c);
393 navit_set_timeout(struct navit *this_)
396 follow.type=attr_follow;
397 follow.u.num=this_->center_timeout;
398 navit_set_attr(this_, &follow);
402 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
406 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
407 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
408 if (this_->ignore_button) {
409 this_->ignore_button=0;
417 this_->button_pressed=1;
421 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
424 navit_set_center_screen(this_, p, 1);
426 popup(this_, button, p);
427 if (button == 4 && this_->use_mousewheel) {
429 navit_zoom_in(this_, 2, p);
431 if (button == 5 && this_->use_mousewheel) {
433 navit_zoom_out(this_, 2, p);
437 this_->button_pressed=0;
438 if (this_->button_timeout) {
439 event_remove_timeout(this_->button_timeout);
440 this_->button_timeout=NULL;
441 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
442 navit_set_center_screen(this_, p, !this_->zoomed);
445 if (this_->motion_timeout) {
446 event_remove_timeout(this_->motion_timeout);
447 this_->motion_timeout=NULL;
454 update_transformation(this_->trans, &this_->pressed, p, &pr);
456 update_transformation(this_->trans, &this_->pressed, p, NULL);
458 graphics_draw_drag(this_->gra, NULL);
459 transform_copy(this_->trans, this_->trans_cursor);
460 graphics_overlay_disable(this_->gra, 0);
462 navit_set_timeout(this_);
471 navit_button(void *data, int pressed, int button, struct point *p)
473 struct navit *this=data;
474 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
475 if (!this->ignore_graphics_events) {
476 if (! this->popup_callback)
477 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
478 navit_handle_button(this, pressed, button, p, this->popup_callback);
484 navit_motion_timeout(struct navit *this_)
488 if (this_->drag_bitmap) {
490 point.x=(this_->current.x-this_->pressed.x);
491 point.y=(this_->current.y-this_->pressed.y);
492 if (graphics_draw_drag(this_->gra, &point)) {
493 graphics_overlay_disable(this_->gra, 1);
494 graphics_draw_mode(this_->gra, draw_mode_end);
496 this_->motion_timeout=NULL;
500 dx=(this_->current.x-this_->last.x);
501 dy=(this_->current.y-this_->last.y);
503 struct transformation *tr;
505 this_->last=this_->current;
506 graphics_overlay_disable(this_->gra, 1);
507 tr=transform_dup(this_->trans);
511 update_transformation(tr, &this_->pressed, &this_->current, &pr);
513 update_transformation(tr, &this_->pressed, &this_->current, NULL);
516 graphics_displaylist_move(this_->displaylist, dx, dy);
518 graphics_draw_cancel(this_->gra, this_->displaylist);
519 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
520 transform_destroy(tr);
523 this_->motion_timeout=NULL;
528 navit_handle_motion(struct navit *this_, struct point *p)
532 if (this_->button_pressed && !this_->popped) {
533 dx=(p->x-this_->pressed.x);
534 dy=(p->y-this_->pressed.y);
535 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
537 if (this_->button_timeout) {
538 event_remove_timeout(this_->button_timeout);
539 this_->button_timeout=NULL;
542 if (! this_->motion_timeout_callback)
543 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
544 if (! this_->motion_timeout)
545 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
551 navit_motion(void *data, struct point *p)
553 struct navit *this=data;
554 if (!this->ignore_graphics_events)
555 navit_handle_motion(this, p);
559 navit_predraw(struct navit *this_)
562 struct navit_vehicle *nv;
563 transform_copy(this_->trans, this_->trans_cursor);
567 navit_vehicle_draw(this_, nv, NULL);
573 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
575 struct coord c1, c2, *center;
576 if (scale < this_->zoom_min)
577 scale=this_->zoom_min;
578 if (scale > this_->zoom_max)
579 scale=this_->zoom_max;
581 transform_reverse(this_->trans, p, &c1);
582 transform_set_scale(this_->trans, scale);
584 transform_reverse(this_->trans, p, &c2);
585 center = transform_center(this_->trans);
586 center->x += c1.x - c2.x;
587 center->y += c1.y - c2.y;
594 * @brief Automatically adjusts zoom level
596 * This function automatically adjusts the current
597 * zoom level according to the current speed.
599 * @param this_ The navit struct
600 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
601 * @param speed The vehicles speed in meters per second
602 * @param dir The direction into which the vehicle moves
605 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
612 if (! this_->autozoom_active) {
616 distance = speed * this_->autozoom_secs;
618 transform_get_size(this_->trans, &w, &h);
619 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
620 scale = transform_get_scale(this_->trans);
622 /* We make sure that the point we want to see is within a certain range
623 * around the vehicle. The radius of this circle is the size of the
624 * screen. This doesn't necessarily mean the point is visible because of
625 * perspective etc. Quite rough, but should be enough. */
628 new_scale = (double)distance / h * 16;
630 new_scale = (double)distance / w * 16;
633 if (abs(new_scale - scale) < 2) {
637 if (new_scale >= this_->autozoom_min) {
638 navit_scale(this_, (long)new_scale, &pc, 0);
640 if (scale != this_->autozoom_min) {
641 navit_scale(this_, this_->autozoom_min, &pc, 0);
647 * Change the current zoom level, zooming closer to the ground
649 * @param navit The navit instance
650 * @param factor The zoom factor, usually 2
651 * @param p The invariant point (if set to NULL, default to center)
655 navit_zoom_in(struct navit *this_, int factor, struct point *p)
657 long scale=transform_get_scale(this_->trans)/factor;
660 navit_scale(this_, scale, p, 1);
664 * Change the current zoom level
666 * @param navit The navit instance
667 * @param factor The zoom factor, usually 2
668 * @param p The invariant point (if set to NULL, default to center)
672 navit_zoom_out(struct navit *this_, int factor, struct point *p)
674 long scale=transform_get_scale(this_->trans)*factor;
675 navit_scale(this_, scale, p, 1);
679 navit_zoom_in_cursor(struct navit *this_, int factor)
682 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
683 navit_zoom_in(this_, factor, &p);
684 this_->vehicle->follow_curr=this_->vehicle->follow;
686 navit_zoom_in(this_, factor, NULL);
690 navit_zoom_out_cursor(struct navit *this_, int factor)
693 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
694 navit_zoom_out(this_, 2, &p);
695 this_->vehicle->follow_curr=this_->vehicle->follow;
697 navit_zoom_out(this_, 2, NULL);
702 navit_cmd_zoom_in(struct navit *this_)
704 navit_zoom_in_cursor(this_, 2);
709 navit_cmd_zoom_out(struct navit *this_)
711 navit_zoom_out_cursor(this_, 2);
717 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
719 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
720 navit_say(this, in[0]->u.str);
723 static GHashTable *cmd_int_var_hash = NULL;
726 * Store key value pair for the command system
728 * @param navit The navit instance
729 * @param function unused (needed to match command function signiture)
730 * @param in input attributes in[0] is the key string, in[1] is the integer value to store
731 * @param out output attributes, unused
732 * @param valid unused
736 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
738 if(!cmd_int_var_hash) {
739 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
742 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
743 (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
744 struct attr*val = g_new(struct attr,1);
745 attr_dup_content(in[1],val);
746 char*key = g_strdup(in[0]->u.str);
747 g_hash_table_insert(cmd_int_var_hash, key, val);
752 * Get value given a key string for the command system
754 * @param navit The navit instance
755 * @param function unused (needed to match command function signiture)
756 * @param in input attribute in[0] is the key string
757 * @param out output attribute, the value for the given key string if exists or 0
758 * @param valid unused
762 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
764 struct attr **list = g_new0(struct attr *,2);
765 if(!cmd_int_var_hash) {
766 struct attr*val = g_new0(struct attr,1);
767 val->type = attr_type_int_begin;
771 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
772 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
777 struct attr*val = g_new0(struct attr,1);
778 val->type = attr_type_int_begin;
787 GList *cmd_int_var_stack = NULL;
790 * Push an integer to the stack for the command system
792 * @param navit The navit instance
793 * @param function unused (needed to match command function signiture)
794 * @param in input attribute in[0] is the integer attibute to push
795 * @param out output attributes, unused
796 * @param valid unused
800 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
802 if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
803 struct attr*val = g_new(struct attr,1);
804 attr_dup_content(in[0],val);
805 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
810 * Pop an integer from the command system's integer stack
812 * @param navit The navit instance
813 * @param function unused (needed to match command function signiture)
814 * @param in input attributes unused
815 * @param out output attribute, the value popped if stack isn't empty or 0
816 * @param valid unused
820 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
822 struct attr **list = g_new0(struct attr *,2);
823 if(!cmd_int_var_stack) {
824 struct attr*val = g_new0(struct attr,1);
825 val->type = attr_type_int_begin;
830 list[0] = cmd_int_var_stack->data;
831 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
838 * Get current size of command system's integer stack
840 * @param navit The navit instance
841 * @param function unused (needed to match command function signiture)
842 * @param in input attributes unused
843 * @param out output attribute, the size of stack
844 * @param valid unused
848 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
850 struct attr *attr = g_new0(struct attr ,1);
851 attr->type = attr_type_int_begin;
852 if(!cmd_int_var_stack) {
856 attr->u.num = g_list_length(cmd_int_var_stack);
858 struct attr **list = g_new0(struct attr *,2);
862 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
866 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
869 char *description=NULL;
874 pc.pro = transform_get_projection(this->trans);
875 if (ATTR_IS_COORD(in[0]->type)) {
876 pc.x=in[0]->u.coord->x;
877 pc.y=in[0]->u.coord->y;
879 } else if (ATTR_IS_PCOORD(in[0]->type)) {
882 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
887 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
893 if (in[0] && ATTR_IS_STRING(in[0]->type))
894 description=in[0]->u.str;
895 navit_set_destination(this, &pc, description, 1);
899 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
902 attr.type=attr_type_string_begin;
905 *out=attr_generic_add_attr(*out, &attr);
909 static struct command_table commands[] = {
910 {"zoom_in",command_cast(navit_cmd_zoom_in)},
911 {"zoom_out",command_cast(navit_cmd_zoom_out)},
912 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
913 {"say",command_cast(navit_cmd_say)},
914 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
915 {"set_destination",command_cast(navit_cmd_set_destination)},
916 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
917 {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
918 {"set_int_var",command_cast(navit_cmd_set_int_var)},
919 {"get_int_var",command_cast(navit_cmd_get_int_var)},
920 {"push_int",command_cast(navit_cmd_push_int)},
921 {"pop_int",command_cast(navit_cmd_pop_int)},
922 {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
926 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
928 command_add_table(this_->attr_cbl, commands, count, this_);
932 navit_new(struct attr *parent, struct attr **attrs)
934 struct navit *this_=g_new0(struct navit, 1);
935 struct pcoord center;
938 enum projection pro=projection_mg;
943 this_->self.type=attr_navit;
944 this_->self.u.navit=this_;
945 this_->attr_cbl=callback_list_new();
947 this_->orientation=-1;
948 this_->tracking_flag=1;
949 this_->recentdest_count=10;
950 this_->osd_configuration=-1;
952 this_->center_timeout = 10;
953 this_->use_mousewheel = 1;
954 this_->autozoom_secs = 10;
955 this_->autozoom_min = 7;
956 this_->autozoom_active = 0;
958 this_->zoom_max = 2097152;
959 this_->follow_cursor = 1;
963 this_->trans = transform_new();
964 this_->trans_cursor = transform_new();
965 transform_from_geo(pro, &g, &co);
970 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
972 this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
976 for (;*attrs; attrs++) {
977 navit_set_attr_do(this_, *attrs, 1);
979 this_->displaylist=graphics_displaylist_new();
980 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
982 this_->messages = messagelist_new(attrs);
988 navit_set_gui(struct navit *this_, struct gui *gui)
993 if (gui_has_main_loop(this_->gui)) {
994 if (! main_loop_gui) {
995 main_loop_gui=this_->gui;
997 dbg(0,"gui with main loop already active, ignoring this instance");
1005 navit_add_message(struct navit *this_, char *message)
1007 message_new(this_->messages, message);
1011 *navit_get_messages(struct navit *this_)
1013 return message_get(this_->messages);
1017 navit_set_graphics(struct navit *this_, struct graphics *gra)
1022 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1023 graphics_add_callback(gra, this_->resize_callback);
1024 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1025 graphics_add_callback(gra, this_->button_callback);
1026 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1027 graphics_add_callback(gra, this_->motion_callback);
1028 this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1029 graphics_add_callback(gra, this_->predraw_callback);
1034 navit_get_graphics(struct navit *this_)
1039 struct vehicleprofile *
1040 navit_get_vehicleprofile(struct navit *this_)
1042 return this_->vehicleprofile;
1046 navit_get_vehicleprofiles(struct navit *this_)
1048 return this_->vehicleprofiles;
1052 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1057 c=transform_center(this_->trans);
1058 transform_to_geo(transform_get_projection(this_->trans), c, &g);
1059 transform_set_projection(this_->trans, pro);
1060 transform_from_geo(pro, &g, c);
1066 * Start the route computing to a given set of coordinates
1068 * @param navit The navit instance
1069 * @param c The coordinate to start routing to
1070 * @param description A label which allows the user to later identify this destination in the former destinations selection
1074 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1077 this_->destination=*c;
1078 this_->destination_valid=1;
1080 this_->destination_valid=0;
1081 char *destination_file = bookmarks_get_destination_file(TRUE);
1082 bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
1083 g_free(destination_file);
1084 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1086 route_set_destination(this_->route, c, async);
1088 if (this_->ready == 3)
1094 * Start the route computing to a given set of coordinates including waypoints
1096 * @param navit The navit instance
1097 * @param c The coordinate to start routing to
1098 * @param description A label which allows the user to later identify this destination in the former destinations selection
1102 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1105 this_->destination=c[count-1];
1106 this_->destination_valid=1;
1108 this_->destination_valid=0;
1109 char *destination_file = bookmarks_get_destination_file(TRUE);
1110 bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
1111 g_free(destination_file);
1112 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1114 route_set_destinations(this_->route, c, count, async);
1116 if (this_->ready == 3)
1122 * @brief Checks if a route is calculated
1124 * This function checks if a route is calculated.
1126 * @param this_ The navit struct whose route should be checked.
1127 * @return True if the route is set, false otherwise.
1130 navit_check_route(struct navit *this_)
1133 return route_get_path_set(this_->route);
1140 navit_former_destinations_active(struct navit *this_)
1142 char *destination_file = bookmarks_get_destination_file(FALSE);
1146 f=fopen(destination_file,"r");
1148 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
1152 g_free(destination_file);
1157 navit_add_former_destinations_from_file(struct navit *this_)
1159 char *destination_file = bookmarks_get_destination_file(FALSE);
1160 struct attr parent={attr_navit, .u.navit=this_};
1161 struct attr type={attr_type, {"textfile"}}, data={attr_data, {destination_file}}, flags={attr_flags, {(void *)1}};
1162 struct attr *attrs[]={&type, &data, &flags, NULL};
1163 struct map_rect *mr;
1165 int i,valid=0,count=0;
1167 struct pcoord pc[16];
1169 this_->former_destination=map_new(&parent, attrs);
1170 g_free(destination_file);
1171 if (!this_->route || !navit_former_destinations_active(this_))
1173 mr=map_rect_new(this_->former_destination, NULL);
1174 while ((item=map_rect_get_item(mr))) {
1175 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
1178 map_rect_destroy(mr);
1179 if (valid && count > 0) {
1180 for (i = 0 ; i < count ; i++) {
1181 pc[i].pro=map_projection(this_->former_destination);
1186 route_set_destination(this_->route, &pc[0], 1);
1188 route_set_destinations(this_->route, pc, count, 1);
1189 this_->destination=pc[count-1];
1190 this_->destination_valid=1;
1196 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1201 if (this_->textfile_debug_log && this_->vehicle) {
1202 str1=g_strdup_vprintf(fmt, ap);
1203 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1204 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1212 navit_speech_estimate(struct navit *this_, char *str)
1214 return speech_estimate_duration(this_->speech, str);
1218 navit_say(struct navit *this_, char *text)
1221 speech_say(this_->speech, text);
1226 * @brief Toggles the navigation announcer for navit
1227 * @param this_ The navit object
1230 navit_cmd_announcer_toggle(struct navit *this_)
1232 struct attr attr, speechattr;
1234 // search for the speech attribute
1235 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1237 // find out if the corresponding attribute attr_active has been set
1238 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1240 attr.u.num = !attr.u.num;
1242 // otherwise disable it because voice is enabled by default
1243 attr.type = attr_active;
1247 // apply the new state
1248 if(!speech_set_attr(speechattr.u.speech, &attr))
1251 // announce that the speech attribute has changed
1252 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1256 navit_speak(struct navit *this_)
1258 struct navigation *nav=this_->navigation;
1259 struct map *map=NULL;
1260 struct map_rect *mr=NULL;
1264 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1266 dbg(1, "this_.speech->active %i\n", attr.u.num);
1271 map=navigation_get_map(nav);
1273 mr=map_rect_new(map, NULL);
1275 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1276 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1277 speech_say(this_->speech, attr.u.str);
1278 navit_add_message(this_, attr.u.str);
1279 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1281 map_rect_destroy(mr);
1286 navit_window_roadbook_update(struct navit *this_)
1288 struct navigation *nav=this_->navigation;
1289 struct map *map=NULL;
1290 struct map_rect *mr=NULL;
1293 struct param_list param[5];
1297 datawindow_mode(this_->roadbook_window, 1);
1299 map=navigation_get_map(nav);
1301 mr=map_rect_new(map, NULL);
1302 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1304 dbg(0,"while loop\n");
1305 while ((item=map_rect_get_item(mr))) {
1306 dbg(0,"item=%p\n", item);
1308 if (item->type != type_nav_position) {
1309 item_attr_get(item, attr_navigation_long, &attr);
1310 if (attr.u.str == NULL) {
1313 dbg(2, "Command='%s'\n", attr.u.str);
1314 param[0].value=g_strdup(attr.u.str);
1316 param[0].value=_("Position");
1317 param[0].name=_("Command");
1319 item_attr_get(item, attr_length, &attr);
1320 dbg(2, "Length=%d\n", attr.u.num);
1321 param[1].name=_("Length");
1323 if ( attr.u.num >= 2000 )
1325 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1329 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1332 item_attr_get(item, attr_time, &attr);
1333 dbg(2, "Time=%d\n", attr.u.num);
1335 param[2].name=_("Time");
1338 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1342 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1345 item_attr_get(item, attr_destination_length, &attr);
1346 dbg(2, "Destlength=%d\n", attr.u.num);
1347 param[3].name=_("Destination Length");
1348 if ( attr.u.num >= 2000 )
1350 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1354 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1357 item_attr_get(item, attr_destination_time, &attr);
1358 dbg(2, "Desttime=%d\n", attr.u.num);
1360 param[4].name=_("Destination Time");
1363 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1367 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1369 datawindow_add(this_->roadbook_window, param, 5);
1371 map_rect_destroy(mr);
1373 datawindow_mode(this_->roadbook_window, 0);
1377 navit_window_roadbook_destroy(struct navit *this_)
1380 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1381 this_->roadbook_window=NULL;
1382 this_->roadbook_callback=NULL;
1385 navit_window_roadbook_new(struct navit *this_)
1387 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1391 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1392 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1393 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1394 navit_window_roadbook_update(this_);
1398 navit_init(struct navit *this_)
1404 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1406 if (!this_->gui && !(this_->flags & 2)) {
1408 navit_destroy(this_);
1411 if (!this_->gra && !(this_->flags & 1)) {
1412 dbg(0,"no graphics\n");
1413 navit_destroy(this_);
1416 dbg(2,"Connecting gui to graphics\n");
1417 if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1418 struct attr attr_type_gui, attr_type_graphics;
1419 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1420 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1421 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1422 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1423 dbg(0," for explanations and solutions\n");
1425 navit_destroy(this_);
1428 dbg(2,"Initializing graphics\n");
1429 dbg(2,"Setting Vehicle\n");
1430 navit_set_vehicle(this_, this_->vehicle);
1431 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1432 if (this_->mapsets) {
1433 struct mapset_handle *msh;
1434 ms=this_->mapsets->data;
1435 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1436 msh=mapset_open(ms);
1437 while (msh && (map=mapset_next(msh, 0))) {
1438 map_add_callback(map, this_->progress_cb);
1443 if ((map=route_get_map(this_->route)))
1444 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1445 if ((map=route_get_graph_map(this_->route))) {
1446 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1447 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1449 route_set_mapset(this_->route, ms);
1450 route_set_projection(this_->route, transform_get_projection(this_->trans));
1452 if (this_->tracking) {
1453 tracking_set_mapset(this_->tracking, ms);
1455 tracking_set_route(this_->tracking, this_->route);
1457 if (this_->navigation) {
1458 if ((map=navigation_get_map(this_->navigation))) {
1459 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1460 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1463 if (this_->tracking) {
1464 if ((map=tracking_get_map(this_->tracking))) {
1465 mapset_add_attr(ms, &(struct attr){attr_map,.u.map=map});
1466 map_set_attr(map, &(struct attr ){attr_active,.u.num=0});
1469 navit_add_former_destinations_from_file(this_);
1472 struct attr callback;
1473 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1474 callback.type=attr_callback;
1475 callback.u.callback=this_->route_cb;
1476 route_add_attr(this_->route, &callback);
1478 if (this_->navigation) {
1479 if (this_->speech) {
1480 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1481 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1484 navigation_set_route(this_->navigation, this_->route);
1486 dbg(2,"Setting Center\n");
1487 char *center_file = bookmarks_get_center_file(FALSE);
1488 bookmarks_set_center_from_file(this_->bookmarks, center_file);
1489 g_free(center_file);
1491 if (this_->menubar) {
1492 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1494 navit_add_menu_windows_items(this_, men);
1500 navit_window_roadbook_new(this_);
1501 navit_window_items_new(this_);
1504 messagelist_init(this_->messages);
1506 navit_set_cursors(this_);
1508 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1509 callback=(this_->ready == 2);
1511 dbg(2,"ready=%d\n",this_->ready);
1512 if (this_->ready == 3)
1515 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1517 routech_test(this_);
1522 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1527 c.x=(r->rl.x+r->lu.x)/2;
1528 c.y=(r->rl.y+r->lu.y)/2;
1529 transform_set_center(this_->trans, &c);
1530 dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1531 while (scale < 1<<20) {
1533 transform_set_scale(this_->trans, scale);
1534 transform_setup_source_rect(this_->trans);
1535 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1536 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1537 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1538 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1539 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1545 if (this_->ready == 3)
1546 navit_draw_async(this_,0);
1550 navit_zoom_to_route(struct navit *this_, int orientation)
1553 struct map_rect *mr=NULL;
1556 struct coord_rect r;
1561 map=route_get_map(this_->route);
1562 dbg(1,"map=%p\n",map);
1564 mr=map_rect_new(map, NULL);
1565 dbg(1,"mr=%p\n",mr);
1567 while ((item=map_rect_get_item(mr))) {
1568 dbg(1,"item=%s\n", item_to_name(item->type));
1569 while (item_coord_get(item, &c, 1)) {
1574 coord_rect_extend(&r, &c);
1581 if (orientation != -1)
1582 transform_set_yaw(this_->trans, orientation);
1583 navit_zoom_to_rect(this_, &r);
1587 navit_cmd_zoom_to_route(struct navit *this)
1589 navit_zoom_to_route(this, 0);
1594 * Change the current zoom level
1596 * @param navit The navit instance
1597 * @param center The point where to center the map, including its projection
1601 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
1603 struct coord *c=transform_center(this_->trans);
1605 enum projection pro = transform_get_projection(this_->trans);
1606 if (pro != center->pro) {
1609 transform_from_to(&c1, center->pro, &c2, pro);
1616 navit_set_timeout(this_);
1617 if (this_->ready == 3)
1622 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
1626 transform_set_center(this_->trans, c);
1627 transform_get_size(this_->trans, &width, &height);
1630 update_transformation(this_->trans, &po, p, NULL);
1632 navit_set_timeout(this_);
1636 * Links all vehicles to a cursor depending on the current profile.
1638 * @param this_ A navit instance
1639 * @author Ralph Sennhauser (10/2009)
1642 navit_set_cursors(struct navit *this_)
1645 struct navit_vehicle *nv;
1649 v=g_list_first(this_->vehicles); // GList of navit_vehicles
1652 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
1653 if (!strcmp(name.u.str,"none"))
1656 c=layout_get_cursor(this_->layout_current, name.u.str);
1658 c=layout_get_cursor(this_->layout_current, "default");
1659 vehicle_set_cursor(nv->vehicle, c);
1666 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
1669 struct navit_vehicle *nv=this_->vehicle;
1671 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
1672 #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 */
1673 float min_offset = 0.; // Percent offset at min_offset_speed.
1674 float max_offset = 30.; // Percent offset at max_offset_speed.
1675 int min_offset_speed = 2; // Speed in km/h
1676 int max_offset_speed = 50; // Speed ini km/h
1677 // Calculate cursor offset from the center of the screen, upon speed.
1678 if (nv->speed <= min_offset_speed) {
1679 offset = min_offset;
1680 } else if (nv->speed > max_offset_speed) {
1681 offset = max_offset;
1683 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
1687 transform_get_size(this_->trans, &width, &height);
1688 if (this_->orientation == -1 || keep_orientation) {
1690 p->y=(50 + offset)*height/100;
1692 *dir=keep_orientation?this_->orientation:nv->dir;
1695 if (this_->tracking && this_->tracking_flag) {
1696 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
1698 mdir=nv->dir-this_->orientation;
1701 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
1702 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
1704 *dir=this_->orientation;
1710 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
1714 struct navit_vehicle *nv=this_->vehicle;
1715 navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
1716 transform_set_yaw(this_->trans, dir);
1717 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
1719 navit_autozoom(this_, &nv->coord, nv->speed, 0);
1723 navit_set_center_cursor_draw(struct navit *this_)
1725 navit_set_center_cursor(this_,1,0);
1726 if (this_->ready == 3)
1727 navit_draw_async(this_, 1);
1731 navit_cmd_set_center_cursor(struct navit *this_)
1733 navit_set_center_cursor_draw(this_);
1737 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
1741 transform_reverse(this_->trans, p, &c);
1744 pc.pro = transform_get_projection(this_->trans);
1745 navit_set_center(this_, &pc, set_timeout);
1749 switch((*attrs)->type) {
1751 zoom=(*attrs)->u.num;
1754 g=*((*attrs)->u.coord_geo);
1759 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
1761 int dir=0, orient_old=0, attr_updated=0;
1765 struct navit_vehicle *nv;
1766 struct attr active=(struct attr){attr_active,{(void *)0}};
1769 switch (attr->type) {
1771 attr_updated=(this_->autozoom_secs != attr->u.num);
1772 this_->autozoom_secs = attr->u.num;
1774 case attr_autozoom_active:
1775 attr_updated=(this_->autozoom_active != attr->u.num);
1776 this_->autozoom_active = attr->u.num;
1779 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
1780 dbg(1,"0x%x,0x%x\n",co.x,co.y);
1781 transform_set_center(this_->trans, &co);
1783 case attr_drag_bitmap:
1784 attr_updated=(this_->drag_bitmap != !!attr->u.num);
1785 this_->drag_bitmap=!!attr->u.num;
1788 attr_updated=(this_->flags != attr->u.num);
1789 this_->flags=attr->u.num;
1791 case attr_flags_graphics:
1792 attr_updated=(this_->graphics_flags != attr->u.num);
1793 this_->graphics_flags=attr->u.num;
1796 if (!this_->vehicle)
1798 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
1799 this_->vehicle->follow_curr = attr->u.num;
1802 if(this_->layout_current!=attr->u.layout) {
1803 this_->layout_current=attr->u.layout;
1804 graphics_font_destroy_all(this_->gra);
1805 navit_set_cursors(this_);
1806 if (this_->ready == 3)
1811 case attr_layout_name:
1815 if (!strcmp(lay->name,attr->u.str)) {
1817 attr.type=attr_layout;
1819 return navit_set_attr_do(this_, &attr, init);
1824 case attr_map_border:
1825 if (this_->border != attr->u.num) {
1826 this_->border=attr->u.num;
1830 case attr_orientation:
1831 orient_old=this_->orientation;
1832 this_->orientation=attr->u.num;
1834 if (this_->orientation != -1) {
1835 dir = this_->orientation;
1837 if (this_->vehicle) {
1838 dir = this_->vehicle->dir;
1841 transform_set_yaw(this_->trans, dir);
1842 if (orient_old != this_->orientation) {
1844 if (this_->ready == 3)
1851 case attr_osd_configuration:
1852 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
1853 attr_updated=(this_->osd_configuration != attr->u.num);
1854 this_->osd_configuration=attr->u.num;
1857 attr_updated=(this_->pitch != attr->u.num);
1858 this_->pitch=attr->u.num;
1859 transform_set_pitch(this_->trans, this_->pitch);
1860 if (!init && attr_updated && this_->ready == 3)
1863 case attr_projection:
1864 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
1865 navit_projection_set(this_, attr->u.projection, !init);
1870 attr_updated=(this_->radius != attr->u.num);
1871 this_->radius=attr->u.num;
1873 case attr_recent_dest:
1874 attr_updated=(this_->recentdest_count != attr->u.num);
1875 this_->recentdest_count=attr->u.num;
1878 if(this_->speech && this_->speech != attr->u.speech) {
1880 this_->speech = attr->u.speech;
1884 attr_updated=(this_->center_timeout != attr->u.num);
1885 this_->center_timeout = attr->u.num;
1888 attr_updated=(this_->tracking_flag != !!attr->u.num);
1889 this_->tracking_flag=!!attr->u.num;
1891 case attr_transformation:
1892 this_->trans=attr->u.transformation;
1894 case attr_use_mousewheel:
1895 attr_updated=(this_->use_mousewheel != !!attr->u.num);
1896 this_->use_mousewheel=!!attr->u.num;
1902 if (nv->vehicle == attr->u.vehicle) {
1903 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
1905 vehicle_set_attr(this_->vehicle->vehicle, &active);
1907 vehicle_set_attr(nv->vehicle, &active);
1910 navit_set_vehicle(this_, nv);
1916 zoom=transform_get_scale(this_->trans);
1917 attr_updated=(zoom != attr->u.num);
1918 transform_set_scale(this_->trans, attr->u.num);
1919 if (attr_updated && !init)
1923 attr_updated=(attr->u.num != this_->zoom_min);
1924 this_->zoom_min=attr->u.num;
1927 attr_updated=(attr->u.num != this_->zoom_max);
1928 this_->zoom_max=attr->u.num;
1931 navit_add_message(this_, attr->u.str);
1933 case attr_follow_cursor:
1934 attr_updated=(this_->follow_cursor != !!attr->u.num);
1935 this_->follow_cursor=!!attr->u.num;
1940 if (attr_updated && !init) {
1941 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
1942 if (attr->type == attr_osd_configuration)
1943 graphics_draw_mode(this_->gra, draw_mode_end);
1949 navit_set_attr(struct navit *this_, struct attr *attr)
1951 return navit_set_attr_do(this_, attr, 0);
1955 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
1957 struct message *msg;
1963 msg = navit_get_messages(this_);
1971 len += strlen(msg->text) + 1;
1974 attr->u.str = g_malloc(len + 1);
1976 msg = navit_get_messages(this_);
1979 g_stpcpy((attr->u.str + offset), msg->text);
1980 offset += strlen(msg->text);
1981 attr->u.str[offset] = '\n';
1987 attr->u.str[len] = '\0';
1989 case attr_bookmark_map:
1990 attr->u.map=bookmarks_get_map(this_->bookmarks);
1992 case attr_bookmarks:
1993 attr->u.bookmarks=this_->bookmarks;
1995 case attr_callback_list:
1996 attr->u.callback_list=this_->attr_cbl;
1998 case attr_destination:
1999 if (! this_->destination_valid)
2001 attr->u.pcoord=&this_->destination;
2003 case attr_displaylist:
2004 attr->u.displaylist=this_->displaylist;
2005 return (attr->u.displaylist != NULL);
2007 if (!this_->vehicle)
2009 attr->u.num=this_->vehicle->follow_curr;
2011 case attr_former_destination_map:
2012 attr->u.map=this_->former_destination;
2015 attr->u.graphics=this_->gra;
2016 ret=(attr->u.graphics != NULL);
2019 attr->u.gui=this_->gui;
2020 ret=(attr->u.gui != NULL);
2025 iter->u.list=g_list_next(iter->u.list);
2027 iter->u.list=this_->layouts;
2031 attr->u.layout=(struct layout *)iter->u.list->data;
2033 attr->u.layout=this_->layout_current;
2037 if (iter && this_->mapsets) {
2038 if (!iter->u.mapset_handle) {
2039 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2041 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2043 mapset_close(iter->u.mapset_handle);
2051 attr->u.mapset=this_->mapsets->data;
2052 ret=(attr->u.mapset != NULL);
2054 case attr_navigation:
2055 attr->u.navigation=this_->navigation;
2057 case attr_orientation:
2058 attr->u.num=this_->orientation;
2060 case attr_osd_configuration:
2061 attr->u.num=this_->osd_configuration;
2064 attr->u.num=transform_get_pitch(this_->trans);
2066 case attr_projection:
2068 attr->u.num=transform_get_projection(this_->trans);
2074 attr->u.route=this_->route;
2077 attr->u.speech=this_->speech;
2080 attr->u.num=this_->tracking_flag;
2082 case attr_trackingo:
2083 attr->u.tracking=this_->tracking;
2085 case attr_transformation:
2086 attr->u.transformation=this_->trans;
2091 iter->u.list=g_list_next(iter->u.list);
2093 iter->u.list=this_->vehicles;
2097 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2099 if(this_->vehicle) {
2100 attr->u.vehicle=this_->vehicle->vehicle;
2106 case attr_vehicleprofile:
2107 attr->u.vehicleprofile=this_->vehicleprofile;
2110 attr->u.num=transform_get_scale(this_->trans);
2112 case attr_autozoom_active:
2113 attr->u.num=this_->autozoom_active;
2115 case attr_follow_cursor:
2116 attr->u.num=this_->follow_cursor;
2126 navit_add_log(struct navit *this_, struct log *log)
2128 struct attr type_attr;
2129 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2131 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2132 char *header = "type=track_tracked\n";
2133 if (this_->textfile_debug_log)
2135 log_set_header(log, header, strlen(header));
2136 this_->textfile_debug_log=log;
2143 navit_add_attr(struct navit *this_, struct attr *attr)
2146 switch (attr->type) {
2148 navit_add_callback(this_, attr->u.callback);
2151 ret=navit_add_log(this_, attr->u.log);
2154 ret=navit_set_gui(this_, attr->u.gui);
2157 ret=navit_set_graphics(this_, attr->u.graphics);
2160 this_->layouts = g_list_append(this_->layouts, attr->u.layout);
2161 if(!this_->layout_current)
2162 this_->layout_current=attr->u.layout;
2165 this_->route=attr->u.route;
2168 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2170 case attr_navigation:
2171 this_->navigation=attr->u.navigation;
2173 case attr_recent_dest:
2174 this_->recentdest_count = attr->u.num;
2177 this_->speech=attr->u.speech;
2180 this_->tracking=attr->u.tracking;
2183 ret=navit_add_vehicle(this_, attr->u.vehicle);
2185 case attr_vehicleprofile:
2186 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2188 case attr_autozoom_min:
2189 this_->autozoom_min = attr->u.num;
2194 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2199 navit_remove_attr(struct navit *this_, struct attr *attr)
2202 switch (attr->type) {
2204 navit_remove_callback(this_, attr->u.callback);
2213 navit_attr_iter_new(void)
2215 return g_new0(struct attr_iter, 1);
2219 navit_attr_iter_destroy(struct attr_iter *iter)
2225 navit_add_callback(struct navit *this_, struct callback *cb)
2227 callback_list_add(this_->attr_cbl, cb);
2231 navit_remove_callback(struct navit *this_, struct callback *cb)
2233 callback_list_remove(this_->attr_cbl, cb);
2237 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2239 * @param navit The navit instance
2244 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2246 struct point cursor_pnt;
2247 enum projection pro;
2254 pro=transform_get_projection(this_->trans_cursor);
2257 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2259 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2264 pro=transform_get_projection(this_->trans);
2265 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2268 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2270 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2276 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2278 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2279 struct pcoord cursor_pc;
2280 struct point cursor_pnt, *pnt=&cursor_pnt;
2281 struct tracking *tracking=NULL;
2282 struct pcoord pc[16];
2283 enum projection pro=transform_get_projection(this_->trans_cursor);
2285 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2287 char *destination_file;
2290 if (this_->ready != 3) {
2291 profile(0,"return 1\n");
2294 navit_layout_switch(this_);
2295 if (this_->vehicle == nv && this_->tracking_flag)
2296 tracking=this_->tracking;
2298 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2299 attr_object=tracking;
2300 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2302 attr_object=nv->vehicle;
2303 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2305 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2306 if (!attr_valid.u.num != attr_position_valid_invalid)
2308 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2309 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2310 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2311 profile(0,"return 2\n");
2314 nv->dir=*attr_dir.u.numd;
2315 nv->speed=*attr_speed.u.numd;
2316 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2317 if (nv != this_->vehicle) {
2318 navit_vehicle_draw(this_, nv, NULL);
2319 profile(0,"return 3\n");
2322 cursor_pc.x = nv->coord.x;
2323 cursor_pc.y = nv->coord.y;
2324 cursor_pc.pro = pro;
2327 route_set_position_from_tracking(this_->route, tracking, pro);
2329 route_set_position(this_->route, &cursor_pc);
2331 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2332 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2333 if (this_->gui && nv->speed > 2)
2334 navit_disable_suspend();
2336 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2337 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2338 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2339 navit_set_center_cursor_draw(this_);
2341 navit_vehicle_draw(this_, nv, pnt);
2343 if (nv->follow_curr > 1)
2346 nv->follow_curr=nv->follow;
2347 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2349 /* Finally, if we reached our destination, stop navigation. */
2351 switch(route_destination_reached(this_->route)) {
2353 route_remove_waypoint(this_->route);
2354 count=route_get_destinations(this_->route, pc, 16);
2355 destination_file = bookmarks_get_destination_file(TRUE);
2356 bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2359 navit_set_destination(this_, NULL, NULL, 0);
2363 profile(0,"return 5\n");
2367 * Set the position of the vehicle
2369 * @param navit The navit instance
2370 * @param c The coordinate to set as position
2375 navit_set_position(struct navit *this_, struct pcoord *c)
2378 route_set_position(this_->route, c);
2379 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2381 if (this_->ready == 3)
2386 navit_set_vehicleprofile(struct navit *this_, char *name)
2390 l=this_->vehicleprofiles;
2392 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2393 if (!strcmp(attr.u.str, name)) {
2394 this_->vehicleprofile=l->data;
2396 route_set_profile(this_->route, this_->vehicleprofile);
2406 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2410 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2411 if (navit_set_vehicleprofile(this_, attr.u.str))
2414 if (!navit_set_vehicleprofile(this_,"car")) {
2415 /* We do not have a fallback "car" profile
2416 * so lets set any profile */
2418 l=this_->vehicleprofiles;
2420 this_->vehicleprofile=l->data;
2422 route_set_profile(this_->route, this_->vehicleprofile);
2428 * Register a new vehicle
2430 * @param navit The navit instance
2431 * @param v The vehicle instance
2432 * @returns 1 for success
2435 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2437 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2438 struct attr follow, active, animate;
2443 nv->animate_cursor=0;
2444 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2445 nv->follow=follow.u.num;
2446 nv->follow_curr=nv->follow;
2447 this_->vehicles=g_list_append(this_->vehicles, nv);
2448 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2449 navit_set_vehicle(this_, nv);
2450 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2451 nv->animate_cursor=animate.u.num;
2452 nv->callback.type=attr_callback;
2453 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2454 vehicle_add_attr(nv->vehicle, &nv->callback);
2455 vehicle_set_attr(nv->vehicle, &this_->self);
2463 navit_get_gui(struct navit *this_)
2468 struct transformation *
2469 navit_get_trans(struct navit *this_)
2471 return this_->trans;
2475 navit_get_route(struct navit *this_)
2477 return this_->route;
2481 navit_get_navigation(struct navit *this_)
2483 return this_->navigation;
2486 struct displaylist *
2487 navit_get_displaylist(struct navit *this_)
2489 return this_->displaylist;
2493 navit_layout_switch(struct navit *n)
2497 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2498 double trise,tset,trise_actual;
2500 int year, month, day;
2502 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2503 return; //No layout - nothing to switch
2507 l=layout_attr.u.layout;
2509 if (l->dayname || l->nightname) {
2510 //Ok, we know that we have profile to switch
2512 //Check that we aren't calculating too fast
2513 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2514 currTs=iso8601_to_secs(iso8601_attr.u.str);
2515 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2517 if (currTs-(n->prevTs)<60) {
2518 //We've have to wait a little
2521 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2523 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2524 return; //No valid fix yet
2526 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2527 //No position - no sun
2531 //We calculate sunrise anyway, cause it is needed both for day and for night
2532 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2533 //near the pole sun never rises/sets, so we should never switch profiles
2534 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2540 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2543 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
2544 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2545 //The sun is rising now!
2546 if (strcmp(l->name,l->dayname)) {
2547 navit_set_layout_by_name(n,l->dayname);
2552 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2553 //near the pole sun never rises/sets, so we should never switch profiles
2554 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2558 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2559 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2560 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
2561 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
2563 if (strcmp(l->name,l->nightname)) {
2564 navit_set_layout_by_name(n,l->nightname);
2574 navit_set_vehicle_by_name(struct navit *n,const char *name)
2577 struct attr_iter *iter;
2578 struct attr vehicle_attr, name_attr;
2580 iter=navit_attr_iter_new();
2582 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
2583 v=vehicle_attr.u.vehicle;
2584 vehicle_get_attr(v,attr_name,&name_attr,NULL);
2585 if (name_attr.type==attr_name) {
2586 if (!strcmp(name,name_attr.u.str)) {
2587 navit_set_attr(n,&vehicle_attr);
2588 navit_attr_iter_destroy(iter);
2593 navit_attr_iter_destroy(iter);
2598 navit_set_layout_by_name(struct navit *n,const char *name)
2601 struct attr_iter iter;
2602 struct attr layout_attr;
2606 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
2607 return 0; //No layouts - nothing to do
2609 if (iter.u.list==NULL) {
2613 iter.u.list=g_list_first(iter.u.list);
2615 while(iter.u.list) {
2616 l=(struct layout*)iter.u.list->data;
2617 if (!strcmp(name,l->name)) {
2618 layout_attr.u.layout=l;
2619 layout_attr.type=attr_layout;
2620 navit_set_attr(n,&layout_attr);
2621 iter.u.list=g_list_first(iter.u.list);
2624 iter.u.list=g_list_next(iter.u.list);
2627 iter.u.list=g_list_first(iter.u.list);
2632 navit_disable_suspend() {
2633 gui_disable_suspend(global_navit->gui);
2634 callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
2638 navit_block(struct navit *this_, int block)
2641 this_->blocked |= 1;
2642 if (graphics_draw_cancel(this_->gra, this_->displaylist))
2643 this_->blocked |= 2;
2646 if ((this_->blocked & 2) || block < 0) {
2656 navit_destroy(struct navit *this_)
2658 callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
2660 /* TODO: destroy objects contained in this_ */
2662 vehicle_destroy(this_->vehicle->vehicle);
2663 if (this_->bookmarks) {
2664 char *center_file = bookmarks_get_center_file(TRUE);
2665 bookmarks_write_center_to_file(this_->bookmarks, center_file);
2666 g_free(center_file);
2667 bookmarks_destroy(this_->bookmarks);
2669 callback_destroy(this_->nav_speech_cb);
2670 callback_destroy(this_->roadbook_callback);
2671 callback_destroy(this_->popup_callback);
2672 callback_destroy(this_->motion_timeout_callback);
2674 graphics_remove_callback(this_->gra, this_->resize_callback);
2675 callback_destroy(this_->resize_callback);
2677 graphics_remove_callback(this_->gra, this_->button_callback);
2678 callback_destroy(this_->button_callback);
2680 graphics_remove_callback(this_->gra, this_->motion_callback);
2681 callback_destroy(this_->motion_callback);
2683 graphics_remove_callback(this_->gra, this_->predraw_callback);
2684 callback_destroy(this_->predraw_callback);
2685 route_destroy(this_->route);