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
39 #include "projection.h"
45 #include "transform.h"
50 #include "data_window.h"
52 #include "navigation.h"
63 #include "navit_nls.h"
67 #include "vehicleprofile.h"
69 #include "bookmarks.h"
70 #ifdef HAVE_API_WIN32_BASE
74 #ifdef HAVE_API_WIN32_CE
78 /*FIXME: don't include pvt textfile map includes here */
79 #include "map/textfile/textfile.h"
82 * @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
86 //! The navit_vehicule
87 struct navit_vehicle {
89 /*! Limit of the follow counter. See navit_add_vehicle */
91 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
95 struct coord last; /*< Position of the last update of this vehicle */
96 struct vehicle *vehicle;
106 struct layout *layout_current;
107 struct graphics *gra;
108 struct action *action;
109 struct transformation *trans, *trans_cursor;
110 struct compass *compass;
112 struct navigation *navigation;
113 struct speech *speech;
114 struct tracking *tracking;
117 struct displaylist *displaylist;
120 int recentdest_count;
121 int osd_configuration;
123 GList *windows_items;
124 struct navit_vehicle *vehicle;
125 struct callback_list *attr_cbl;
126 struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb, *progress_cb;
127 struct datawindow *roadbook_window;
128 struct map *former_destination;
129 struct point pressed, last, current;
130 int button_pressed,moved,popped,zoomed;
135 struct event_timeout *button_timeout, *motion_timeout;
136 struct callback *motion_timeout_callback;
138 int ignore_graphics_events;
139 struct log *textfile_debug_log;
140 struct pcoord destination;
141 int destination_valid;
146 struct messagelist *messages;
147 struct callback *resize_callback,*button_callback,*motion_callback,*predraw_callback;
148 struct vehicleprofile *vehicleprofile;
149 GList *vehicleprofiles;
154 int zoom_min, zoom_max;
156 struct bookmarks *bookmarks;
158 /* 1=No graphics ok */
162 struct attr **attr_list;
165 struct gui *main_loop_gui;
171 struct mapset_handle *mapset_handle;
175 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
176 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
177 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
178 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
179 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
180 static void navit_set_cursors(struct navit *this_);
181 static void navit_cmd_zoom_to_route(struct navit *this);
182 static void navit_cmd_set_center_cursor(struct navit *this_);
183 static void navit_cmd_announcer_toggle(struct navit *this_);
184 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
186 struct navit *global_navit;
189 navit_add_mapset(struct navit *this_, struct mapset *ms)
191 this_->mapsets = g_list_append(this_->mapsets, ms);
195 navit_get_mapset(struct navit *this_)
198 return this_->mapsets->data;
200 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
206 navit_get_tracking(struct navit *this_)
208 return this_->tracking;
212 * @brief Get the user data directory.
213 * @param[in] create - create the directory if it does not exist
215 * @return char * to the data directory string.
217 * returns the directory used to store user data files (center.txt,
218 * destination.txt, bookmark.txt, ...)
222 navit_get_user_data_directory(int create) {
224 dir = getenv("NAVIT_USER_DATADIR");
225 if (create && !file_exists(dir)) {
226 dbg(0,"creating dir %s\n", dir);
227 if (file_mkdir(dir,0)) {
228 dbg(0,"failed creating dir %s\n", dir);
233 } /* end: navit_get_user_data_directory(gboolean create) */
237 navit_draw_async(struct navit *this_, int async)
240 if (this_->blocked) {
244 transform_setup_source_rect(this_->trans);
245 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
249 navit_draw(struct navit *this_)
251 if (this_->ready == 3)
252 navit_draw_async(this_, 0);
256 navit_get_ready(struct navit *this_)
264 navit_draw_displaylist(struct navit *this_)
266 if (this_->ready == 3)
267 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
271 navit_map_progress(struct navit *this_)
275 struct mapset_handle *msh;
278 if (this_->ready != 3)
283 ms=this_->mapsets->data;
285 while (msh && (map=mapset_next(msh, 0))) {
286 if (map_get_attr(map, attr_progress, &attr, NULL)) {
287 char *str=g_strdup_printf("%s ",attr.u.str);
288 graphics_draw_mode(this_->gra, draw_mode_begin);
289 graphics_draw_text_std(this_->gra, 16, str, &p);
292 graphics_draw_mode(this_->gra, draw_mode_end);
299 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
302 if (attr->type != attr_route_status)
305 if (this_->ready != 3)
307 if (updated != route_status_path_done_new)
309 if (this_->vehicle) {
310 if (this_->vehicle->follow_curr == 1)
312 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
313 this_->vehicle->follow_curr=this_->vehicle->follow;
319 navit_handle_resize(struct navit *this_, int w, int h)
321 struct map_selection sel;
322 int callback=(this_->ready == 1);
324 memset(&sel, 0, sizeof(sel));
329 transform_set_screen_selection(this_->trans, &sel);
330 graphics_init(this_->gra);
331 graphics_set_rect(this_->gra, &sel.u.p_rect);
333 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
334 if (this_->ready == 3)
335 navit_draw_async(this_, 1);
339 navit_resize(void *data, int w, int h)
341 struct navit *this=data;
342 if (!this->ignore_graphics_events)
343 navit_handle_resize(this, w, h);
347 navit_get_width(struct navit *this_)
354 navit_get_height(struct navit *this_)
360 navit_popup(void *data)
362 struct navit *this_=data;
363 popup(this_, 1, &this_->pressed);
364 this_->button_timeout=NULL;
370 navit_ignore_button(struct navit *this_)
372 if (this_->ignore_button)
374 this_->ignore_button=1;
379 navit_ignore_graphics_events(struct navit *this_, int ignore)
381 this_->ignore_graphics_events=ignore;
385 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
390 double angleo,anglen;
392 if (!transform_reverse(tr, old, &co))
395 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
396 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
397 yaw=transform_get_yaw(tr)+angleo-anglen;
398 transform_set_yaw(tr, yaw % 360);
400 if (!transform_reverse(tr, new, &cn))
402 cp=transform_get_center(tr);
405 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
406 transform_set_center(tr, &c);
410 navit_set_timeout(struct navit *this_)
413 follow.type=attr_follow;
414 follow.u.num=this_->center_timeout;
415 navit_set_attr(this_, &follow);
419 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
423 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
424 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
425 if (this_->ignore_button) {
426 this_->ignore_button=0;
434 this_->button_pressed=1;
438 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
441 navit_set_center_screen(this_, p, 1);
443 popup(this_, button, p);
444 if (button == 4 && this_->use_mousewheel) {
446 navit_zoom_in(this_, 2, p);
448 if (button == 5 && this_->use_mousewheel) {
450 navit_zoom_out(this_, 2, p);
454 this_->button_pressed=0;
455 if (this_->button_timeout) {
456 event_remove_timeout(this_->button_timeout);
457 this_->button_timeout=NULL;
458 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
459 navit_set_center_screen(this_, p, !this_->zoomed);
462 if (this_->motion_timeout) {
463 event_remove_timeout(this_->motion_timeout);
464 this_->motion_timeout=NULL;
471 update_transformation(this_->trans, &this_->pressed, p, &pr);
473 update_transformation(this_->trans, &this_->pressed, p, NULL);
475 graphics_draw_drag(this_->gra, NULL);
476 transform_copy(this_->trans, this_->trans_cursor);
477 graphics_overlay_disable(this_->gra, 0);
479 navit_set_timeout(this_);
488 navit_button(void *data, int pressed, int button, struct point *p)
490 struct navit *this=data;
491 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
492 if (!this->ignore_graphics_events) {
493 if (! this->popup_callback)
494 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
495 navit_handle_button(this, pressed, button, p, this->popup_callback);
501 navit_motion_timeout(struct navit *this_)
505 if (this_->drag_bitmap) {
507 point.x=(this_->current.x-this_->pressed.x);
508 point.y=(this_->current.y-this_->pressed.y);
509 if (graphics_draw_drag(this_->gra, &point)) {
510 graphics_overlay_disable(this_->gra, 1);
511 graphics_draw_mode(this_->gra, draw_mode_end);
513 this_->motion_timeout=NULL;
517 dx=(this_->current.x-this_->last.x);
518 dy=(this_->current.y-this_->last.y);
520 struct transformation *tr;
522 this_->last=this_->current;
523 graphics_overlay_disable(this_->gra, 1);
524 tr=transform_dup(this_->trans);
528 update_transformation(tr, &this_->pressed, &this_->current, &pr);
530 update_transformation(tr, &this_->pressed, &this_->current, NULL);
533 graphics_displaylist_move(this_->displaylist, dx, dy);
535 graphics_draw_cancel(this_->gra, this_->displaylist);
536 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
537 transform_destroy(tr);
540 this_->motion_timeout=NULL;
545 navit_handle_motion(struct navit *this_, struct point *p)
549 if (this_->button_pressed && !this_->popped) {
550 dx=(p->x-this_->pressed.x);
551 dy=(p->y-this_->pressed.y);
552 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
554 if (this_->button_timeout) {
555 event_remove_timeout(this_->button_timeout);
556 this_->button_timeout=NULL;
559 if (! this_->motion_timeout_callback)
560 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
561 if (! this_->motion_timeout)
562 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
568 navit_motion(void *data, struct point *p)
570 struct navit *this=data;
571 if (!this->ignore_graphics_events)
572 navit_handle_motion(this, p);
576 navit_predraw(struct navit *this_)
579 struct navit_vehicle *nv;
580 transform_copy(this_->trans, this_->trans_cursor);
584 navit_vehicle_draw(this_, nv, NULL);
590 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
592 struct coord c1, c2, *center;
593 if (scale < this_->zoom_min)
594 scale=this_->zoom_min;
595 if (scale > this_->zoom_max)
596 scale=this_->zoom_max;
598 transform_reverse(this_->trans, p, &c1);
599 transform_set_scale(this_->trans, scale);
601 transform_reverse(this_->trans, p, &c2);
602 center = transform_center(this_->trans);
603 center->x += c1.x - c2.x;
604 center->y += c1.y - c2.y;
611 * @brief Automatically adjusts zoom level
613 * This function automatically adjusts the current
614 * zoom level according to the current speed.
616 * @param this_ The navit struct
617 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
618 * @param speed The vehicles speed in meters per second
619 * @param dir The direction into which the vehicle moves
622 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
629 if (! this_->autozoom_active) {
633 distance = speed * this_->autozoom_secs;
635 transform_get_size(this_->trans, &w, &h);
636 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
637 scale = transform_get_scale(this_->trans);
639 /* We make sure that the point we want to see is within a certain range
640 * around the vehicle. The radius of this circle is the size of the
641 * screen. This doesn't necessarily mean the point is visible because of
642 * perspective etc. Quite rough, but should be enough. */
645 new_scale = (double)distance / h * 16;
647 new_scale = (double)distance / w * 16;
650 if (abs(new_scale - scale) < 2) {
654 if (new_scale >= this_->autozoom_min) {
655 navit_scale(this_, (long)new_scale, &pc, 0);
657 if (scale != this_->autozoom_min) {
658 navit_scale(this_, this_->autozoom_min, &pc, 0);
664 * Change the current zoom level, zooming closer to the ground
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_in(struct navit *this_, int factor, struct point *p)
674 long scale=transform_get_scale(this_->trans)/factor;
677 navit_scale(this_, scale, p, 1);
681 * Change the current zoom level
683 * @param navit The navit instance
684 * @param factor The zoom factor, usually 2
685 * @param p The invariant point (if set to NULL, default to center)
689 navit_zoom_out(struct navit *this_, int factor, struct point *p)
691 long scale=transform_get_scale(this_->trans)*factor;
692 navit_scale(this_, scale, p, 1);
696 navit_zoom_in_cursor(struct navit *this_, int factor)
699 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
700 navit_zoom_in(this_, factor, &p);
701 this_->vehicle->follow_curr=this_->vehicle->follow;
703 navit_zoom_in(this_, factor, NULL);
707 navit_zoom_out_cursor(struct navit *this_, int factor)
710 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
711 navit_zoom_out(this_, 2, &p);
712 this_->vehicle->follow_curr=this_->vehicle->follow;
714 navit_zoom_out(this_, 2, NULL);
718 navit_cmd_zoom_in(struct navit *this_)
720 navit_zoom_in_cursor(this_, 2);
725 navit_cmd_zoom_out(struct navit *this_)
727 navit_zoom_out_cursor(this_, 2);
733 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
735 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
736 navit_say(this, in[0]->u.str);
739 static GHashTable *cmd_int_var_hash = NULL;
740 static GHashTable *cmd_attr_var_hash = NULL;
743 * Store key value pair for the command system (for int typed values)
745 * @param navit The navit instance
746 * @param function unused (needed to match command function signiture)
747 * @param in input attributes in[0] is the key string, in[1] is the integer value to store
748 * @param out output attributes, unused
749 * @param valid unused
753 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
757 if(!cmd_int_var_hash) {
758 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
761 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
762 (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
763 val = g_new(struct attr,1);
764 attr_dup_content(in[1],val);
765 key = g_strdup(in[0]->u.str);
766 g_hash_table_insert(cmd_int_var_hash, key, val);
772 * Store key value pair for the command system (for attr typed values, can be used as opaque handles)
774 * @param navit The navit instance
775 * @param function unused (needed to match command function signiture)
776 * @param in input attributes in[0] is the key string, in[1] is the attr* value to store
777 * @param out output attributes, unused
778 * @param valid unused
781 //TODO free stored attributes on navit_destroy
783 navit_cmd_set_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
787 if(!cmd_attr_var_hash) {
788 cmd_attr_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
791 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
793 val = attr_dup(in[1]);
795 key = g_strdup(in[0]->u.str);
796 g_hash_table_insert(cmd_attr_var_hash, key, val);
798 dbg(1, "Wrong parameters for set_attr_var() command function\n");
805 * command to toggle the active state of a named layer of the current layout
807 * @param navit The navit instance
808 * @param function unused (needed to match command function signiture)
809 * @param in input attribute in[0] is the name of the layer
810 * @param out output unused
811 * @param valid unused
815 navit_cmd_toggle_layer(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
817 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
818 if(this->layout_current && this->layout_current->layers) {
819 GList* layers = this->layout_current->layers;
821 struct layer*l=layers->data;
822 if(l && !strcmp(l->name,in[0]->u.str) ) {
827 layers=g_list_next(layers);
834 * adds an item with the current coordinate of the vehicle to a named map
836 * @param navit The navit instance
837 * @param function unused (needed to match command function signiture)
838 * @param in input attribute in[0] is the name of the map
839 * @param out output attribute, 0 on error or the id of the created item on success
840 * @param valid unused
844 navit_cmd_map_add_curr_pos(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
846 struct attr **list = g_new0(struct attr *,2);
847 struct attr*val = g_new0(struct attr,1);
849 struct map_selection sel;
850 const int selection_range = 10;
851 enum item_type item_type;
853 struct map* curr_map = NULL;
854 struct coord curr_coord;
857 //return invalid item on error
858 val->type = attr_none;
864 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str && //map name
865 in[1] && ATTR_IS_STRING(in[1]->type) && in[1]->u.str //item type
868 if(!(ms=navit_get_mapset(this))) {
869 dbg(1, "Command function map_add_curr_pos(): there is no active mapset\n");
873 if((item_type = item_from_name(in[1]->u.str))==type_none) {
874 dbg(1, "Command function map_add_curr_pos(): unknown item type\n");
878 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
880 //no map with the given name found
882 dbg(1, "Command function map_add_curr_pos(): map not found\n");
886 if(this->vehicle && this->vehicle->vehicle ) {
887 struct attr pos_attr;
888 if(vehicle_get_attr(this->vehicle->vehicle,attr_position_coord_geo,&pos_attr,NULL)) {
889 transform_from_geo(projection_mg, pos_attr.u.coord_geo, &curr_coord);
891 dbg(1, "Command function map_add_curr_pos(): vehicle position is not accessible\n");
895 dbg(1, "Command function map_add_curr_pos(): no vehicle\n");
901 sel.range.min=type_none;
902 sel.range.max=type_tec_common;
903 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
904 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
905 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
906 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
908 mr = map_rect_new(curr_map, &sel);
910 it = map_rect_create_item( mr, item_type);
912 val->type = attr_type_item_begin;
913 item_coord_set(it,&curr_coord, 1, change_mode_modify);
917 map_rect_destroy(mr);
922 * sets an attribute (name value pair) of a map item specified by map name and item id
924 * @param navit The navit instance
925 * @param function unused (needed to match command function signiture)
926 * @param in input attribute in[0] - name of the map ; in[1] - item ; in[2] - attr name ; in[3] - attr value
927 * @param out output attribute, 0 on error, 1 on success
928 * @param valid unused
932 navit_cmd_map_item_set_attr(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
935 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str &&//map name
936 in[1] && ATTR_IS_ITEM(in[1]->type) && //item
937 in[2] && ATTR_IS_STRING(in[2]->type) && in[2]->u.str && //attr_type str
938 in[3] && ATTR_IS_STRING(in[3]->type) && in[3]->u.str //attr_value str
940 struct attr attr_to_set;
941 struct map* curr_map = NULL;
943 struct map_selection sel;
944 const int selection_range = 500;
945 struct coord curr_coord;
948 if(ATTR_IS_STRING(attr_from_name(in[2]->u.str))) {
949 attr_to_set.u.str = in[3]->u.str;
950 attr_to_set.type = attr_from_name(in[2]->u.str);
952 else if(ATTR_IS_INT(attr_from_name(in[2]->u.str))) {
953 attr_to_set.u.num = atoi(in[3]->u.str);
954 attr_to_set.type = attr_from_name(in[2]->u.str);
956 else if(ATTR_IS_DOUBLE(attr_from_name(in[2]->u.str))) {
957 double* val = g_new0(double,1);
958 *val = atof(in[3]->u.str);
959 attr_to_set.u.numd = val;
960 attr_to_set.type = attr_from_name(in[2]->u.str);
963 ms = navit_get_mapset(this);
965 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
972 sel.range.min=type_none;
973 sel.range.max=type_tec_common;
974 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
975 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
976 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
977 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
981 item_attr_set(it, &attr_to_set, change_mode_modify);
984 dbg(4,"Error in command function item_set_attr()\n");
985 dbg(4,"Command function item_set_attr(): map cond: %d\n",(in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)?1:0);
986 dbg(4,"Command function item_set_attr(): item cond: %d\n",(in[1] && ATTR_IS_ITEM(in[1]->type))?1:0);
987 dbg(4,"Command function item_set_attr(): attr type cond: %d\n",(in[2] && ATTR_IS_STRING(in[2]->type) && in[2]->u.str)?1:0);
988 dbg(4,"Command function item_set_attr(): attr val cond: %d\n",(in[3] && ATTR_IS_STRING(in[3]->type) && in[3]->u.str)?1:0);
993 * Get attr variable given a key string for the command system (for opaque usage)
995 * @param navit The navit instance
996 * @param function unused (needed to match command function signiture)
997 * @param in input attribute in[0] is the key string
998 * @param out output attribute, the attr for the given key string if exists or NULL
999 * @param valid unused
1003 navit_cmd_get_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1005 struct attr **list = g_new0(struct attr *,2);
1006 if(!cmd_attr_var_hash) {
1007 struct attr*val = g_new0(struct attr,1);
1008 cmd_attr_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
1009 val->type = attr_type_item_begin;
1013 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
1014 struct attr*ret = g_hash_table_lookup(cmd_attr_var_hash, in[0]->u.str);
1016 list[0] = attr_dup(ret);
1019 struct attr*val = g_new0(struct attr,1);
1020 val->type = attr_type_int_begin;
1031 * Get value given a key string for the command system
1033 * @param navit The navit instance
1034 * @param function unused (needed to match command function signiture)
1035 * @param in input attribute in[0] is the key string
1036 * @param out output attribute, the value for the given key string if exists or 0
1037 * @param valid unused
1041 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1043 struct attr **list = g_new0(struct attr *,2);
1044 if(!cmd_int_var_hash) {
1045 struct attr*val = g_new0(struct attr,1);
1046 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
1047 val->type = attr_type_int_begin;
1051 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
1052 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
1054 list[0] = attr_dup(ret);
1057 struct attr*val = g_new0(struct attr,1);
1058 val->type = attr_type_int_begin;
1067 GList *cmd_int_var_stack = NULL;
1070 * Push an integer to the stack for the command system
1072 * @param navit The navit instance
1073 * @param function unused (needed to match command function signiture)
1074 * @param in input attribute in[0] is the integer attibute to push
1075 * @param out output attributes, unused
1076 * @param valid unused
1080 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1082 if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
1083 struct attr*val = g_new(struct attr,1);
1084 attr_dup_content(in[0],val);
1085 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
1090 * Pop an integer from the command system's integer stack
1092 * @param navit The navit instance
1093 * @param function unused (needed to match command function signiture)
1094 * @param in input attributes unused
1095 * @param out output attribute, the value popped if stack isn't empty or 0
1096 * @param valid unused
1100 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1102 struct attr **list = g_new0(struct attr *,2);
1103 if(!cmd_int_var_stack) {
1104 struct attr*val = g_new0(struct attr,1);
1105 val->type = attr_type_int_begin;
1110 list[0] = cmd_int_var_stack->data;
1111 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1118 * Get current size of command system's integer stack
1120 * @param navit The navit instance
1121 * @param function unused (needed to match command function signiture)
1122 * @param in input attributes unused
1123 * @param out output attribute, the size of stack
1124 * @param valid unused
1128 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1131 struct attr *attr = g_new0(struct attr ,1);
1132 attr->type = attr_type_int_begin;
1133 if(!cmd_int_var_stack) {
1137 attr->u.num = g_list_length(cmd_int_var_stack);
1139 list = g_new0(struct attr *,2);
1143 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1146 static struct attr **
1147 navit_get_coord(struct navit *this, struct attr **in, struct pcoord *pc)
1153 pc->pro = transform_get_projection(this->trans);
1154 if (ATTR_IS_STRING(in[0]->type)) {
1156 coord_parse(in[0]->u.str, pc->pro, &c);
1160 } else if (ATTR_IS_COORD(in[0]->type)) {
1161 pc->x=in[0]->u.coord->x;
1162 pc->y=in[0]->u.coord->y;
1164 } else if (ATTR_IS_PCOORD(in[0]->type)) {
1165 *pc=*in[0]->u.pcoord;
1167 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
1168 pc->pro=in[0]->u.num;
1172 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
1182 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1185 char *description=NULL;
1186 in=navit_get_coord(this, in, &pc);
1189 if (in[0] && ATTR_IS_STRING(in[0]->type))
1190 description=in[0]->u.str;
1191 navit_set_destination(this, &pc, description, 1);
1196 navit_cmd_set_center(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1199 in=navit_get_coord(this, in, &pc);
1202 navit_set_center(this, &pc, 0);
1207 navit_cmd_set_position(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1210 in=navit_get_coord(this, in, &pc);
1213 navit_set_position(this, &pc);
1218 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1221 attr.type=attr_type_string_begin;
1222 attr.u.str="Fix me";
1224 *out=attr_generic_add_attr(*out, &attr);
1229 * Join several string attributes into one
1231 * @param navit The navit instance
1232 * @param function unused (needed to match command function signiture)
1233 * @param in input attributes in[0] - separator, in[1..] - attributes to join
1234 * @param out output attribute joined attribute as string
1235 * @param valid unused
1239 navit_cmd_strjoin(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1244 attr.type=attr_type_string_begin;
1246 if(in[0] && in[1]) {
1247 sep=attr_to_text(in[0],NULL,1);
1248 ret=attr_to_text(in[1],NULL,1);
1249 for(i=2;in[i];i++) {
1250 gchar *in_i=attr_to_text(in[i],NULL,1);
1251 gchar *r=g_strjoin(sep,ret,in_i,NULL);
1259 *out=attr_generic_add_attr(*out, &attr);
1266 * Call external program
1268 * @param navit The navit instance
1269 * @param function unused (needed to match command function signiture)
1270 * @param in input attributes in[0] - name of executable, in[1..] - parameters
1271 * @param out output attribute unused
1272 * @param valid unused
1276 navit_cmd_spawn(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1278 int i,j, nparms, nvalid;
1279 const char ** argv=NULL;
1280 struct spawn_process_info *pi;
1286 if (in[nparms]->type!=attr_none)
1293 argv=g_new(char*,nvalid+1);
1294 for(i=0,j=0;in[i];i++) {
1295 if(in[i]->type!=attr_none ) {
1296 argv[j++]=attr_to_text(in[i],NULL,1);
1298 dbg(0,"Parameter #%i is attr_none - skipping\n",i);
1302 pi=spawn_process(argv);
1304 // spawn_process() testing suite - uncomment following code to test.
1306 // example of non-blocking wait
1307 //int st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1308 // example of blocking wait
1309 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1310 // example of wait after process is finished and status is
1312 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1313 // example of wait after process is finished and status is
1314 // already tested - unblocked
1315 //st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1317 // End testing suite
1318 spawn_process_info_free(pi);
1319 for(i=0;argv[i];i++)
1326 static struct command_table commands[] = {
1327 {"zoom_in",command_cast(navit_cmd_zoom_in)},
1328 {"zoom_out",command_cast(navit_cmd_zoom_out)},
1329 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
1330 {"say",command_cast(navit_cmd_say)},
1331 {"set_center",command_cast(navit_cmd_set_center)},
1332 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
1333 {"set_destination",command_cast(navit_cmd_set_destination)},
1334 {"set_position",command_cast(navit_cmd_set_position)},
1335 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
1336 {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
1337 {"set_int_var",command_cast(navit_cmd_set_int_var)},
1338 {"get_int_var",command_cast(navit_cmd_get_int_var)},
1339 {"push_int",command_cast(navit_cmd_push_int)},
1340 {"pop_int",command_cast(navit_cmd_pop_int)},
1341 {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
1342 {"toggle_layer",command_cast(navit_cmd_toggle_layer)},
1343 {"strjoin",command_cast(navit_cmd_strjoin)},
1344 {"spawn",command_cast(navit_cmd_spawn)},
1345 {"map_add_curr_pos",command_cast(navit_cmd_map_add_curr_pos)},
1346 {"map_item_set_attr",command_cast(navit_cmd_map_item_set_attr)},
1347 {"set_attr_var",command_cast(navit_cmd_set_attr_var)},
1348 {"get_attr_var",command_cast(navit_cmd_get_attr_var)},
1352 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
1354 command_add_table(this_->attr_cbl, commands, count, this_);
1358 navit_new(struct attr *parent, struct attr **attrs)
1360 struct navit *this_=g_new0(struct navit, 1);
1361 struct pcoord center;
1364 enum projection pro=projection_mg;
1369 this_->self.type=attr_navit;
1370 this_->self.u.navit=this_;
1371 this_->attr_cbl=callback_list_new();
1373 this_->orientation=-1;
1374 this_->tracking_flag=1;
1375 this_->recentdest_count=10;
1376 this_->osd_configuration=-1;
1378 this_->center_timeout = 10;
1379 this_->use_mousewheel = 1;
1380 this_->autozoom_secs = 10;
1381 this_->autozoom_min = 7;
1382 this_->autozoom_active = 0;
1383 this_->zoom_min = 1;
1384 this_->zoom_max = 2097152;
1385 this_->follow_cursor = 1;
1389 this_->trans = transform_new();
1390 this_->trans_cursor = transform_new();
1391 transform_from_geo(pro, &g, &co);
1396 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
1398 this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
1402 for (;*attrs; attrs++) {
1403 navit_set_attr_do(this_, *attrs, 1);
1405 this_->displaylist=graphics_displaylist_new();
1406 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
1408 this_->messages = messagelist_new(attrs);
1414 navit_set_gui(struct navit *this_, struct gui *gui)
1419 if (gui_has_main_loop(this_->gui)) {
1420 if (! main_loop_gui) {
1421 main_loop_gui=this_->gui;
1423 dbg(0,"gui with main loop already active, ignoring this instance");
1431 navit_add_message(struct navit *this_, char *message)
1433 message_new(this_->messages, message);
1437 *navit_get_messages(struct navit *this_)
1439 return message_get(this_->messages);
1443 navit_set_graphics(struct navit *this_, struct graphics *gra)
1448 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1449 graphics_add_callback(gra, this_->resize_callback);
1450 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1451 graphics_add_callback(gra, this_->button_callback);
1452 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1453 graphics_add_callback(gra, this_->motion_callback);
1454 this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1455 graphics_add_callback(gra, this_->predraw_callback);
1460 navit_get_graphics(struct navit *this_)
1465 struct vehicleprofile *
1466 navit_get_vehicleprofile(struct navit *this_)
1468 return this_->vehicleprofile;
1472 navit_get_vehicleprofiles(struct navit *this_)
1474 return this_->vehicleprofiles;
1478 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1483 c=transform_center(this_->trans);
1484 transform_to_geo(transform_get_projection(this_->trans), c, &g);
1485 transform_set_projection(this_->trans, pro);
1486 transform_from_geo(pro, &g, c);
1492 navit_mark_navigation_stopped(char *former_destination_file){
1494 f=fopen(former_destination_file, "a");
1496 fprintf(f,"%s", TEXTFILE_COMMENT_NAVI_STOPPED);
1499 dbg(0, "Error setting mark in destination file %s: %s\n", former_destination_file, strerror(errno));
1504 * Start the route computing to a given set of coordinates
1506 * @param navit The navit instance
1507 * @param c The coordinate to start routing to
1508 * @param description A label which allows the user to later identify this destination in the former destinations selection
1512 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1514 char *destination_file;
1515 destination_file = bookmarks_get_destination_file(TRUE);
1517 this_->destination=*c;
1518 this_->destination_valid=1;
1520 dbg(1, "c=(%i,%i)\n", c->x,c->y);
1521 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_destination, description, this_->recentdest_count);
1523 this_->destination_valid=0;
1524 navit_mark_navigation_stopped(destination_file);
1526 g_free(destination_file);
1527 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1529 route_set_destination(this_->route, c, async);
1531 if (this_->ready == 3)
1537 * Start the route computing to a given set of coordinates including waypoints
1539 * @param navit The navit instance
1540 * @param c The coordinate to start routing to
1541 * @param description A label which allows the user to later identify this destination in the former destinations selection
1545 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1547 char *destination_file;
1549 this_->destination=c[count-1];
1550 this_->destination_valid=1;
1552 destination_file = bookmarks_get_destination_file(TRUE);
1553 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_itinerary, description, this_->recentdest_count);
1554 g_free(destination_file);
1556 this_->destination_valid=0;
1557 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1559 route_set_destinations(this_->route, c, count, async);
1561 if (this_->ready == 3)
1567 * @brief Checks if a route is calculated
1569 * This function checks if a route is calculated.
1571 * @param this_ The navit struct whose route should be checked.
1572 * @return True if the route is set, false otherwise.
1575 navit_check_route(struct navit *this_)
1578 return route_get_path_set(this_->route);
1585 navit_former_destinations_active(struct navit *this_)
1587 char *destination_file_name = bookmarks_get_destination_file(FALSE);
1588 FILE *destination_file;
1591 destination_file=fopen(destination_file_name,"r");
1592 if (destination_file) {
1593 while(fgets(lastline, sizeof(lastline), destination_file));
1594 fclose(destination_file);
1595 if ((lastline != NULL) && (strcmp(lastline, TEXTFILE_COMMENT_NAVI_STOPPED))){
1599 g_free(destination_file_name);
1603 struct map* read_former_destinations_from_file(){
1604 struct attr type, data, flags, *attrs[4];
1605 char *destination_file = bookmarks_get_destination_file(FALSE);
1608 type.type=attr_type;
1609 type.u.str="textfile";
1611 data.type=attr_data;
1612 data.u.str=destination_file;
1614 flags.type=attr_flags;
1617 attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1619 m=map_new(NULL, attrs);
1620 g_free(destination_file);
1625 navit_add_former_destinations_from_file(struct navit *this_)
1628 int i,valid=0,count=0;
1630 struct pcoord pc[16];
1631 struct map_rect *mr;
1633 this_->former_destination=read_former_destinations_from_file();
1634 if (!this_->route || !navit_former_destinations_active(this_))
1636 mr=map_rect_new(this_->former_destination, NULL);
1637 while ((item=map_rect_get_item(mr))) {
1638 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
1641 map_rect_destroy(mr);
1642 if (valid && count > 0) {
1643 for (i = 0 ; i < count ; i++) {
1644 pc[i].pro=map_projection(this_->former_destination);
1649 route_set_destination(this_->route, &pc[0], 1);
1651 route_set_destinations(this_->route, pc, count, 1);
1652 this_->destination=pc[count-1];
1653 this_->destination_valid=1;
1659 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1664 if (this_->textfile_debug_log && this_->vehicle) {
1665 str1=g_strdup_vprintf(fmt, ap);
1666 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1667 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1675 navit_textfile_debug_log_at(struct navit *this_, struct pcoord *pc, const char *fmt, ...)
1680 if (this_->textfile_debug_log && this_->vehicle) {
1681 str1=g_strdup_vprintf(fmt, ap);
1682 str2=g_strdup_printf("0x%x 0x%x%s%s\n", pc->x, pc->y, strlen(str1) ? " " : "", str1);
1683 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1691 navit_say(struct navit *this_, char *text)
1695 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1697 dbg(1, "this_.speech->active %i\n", attr.u.num);
1699 speech_say(this_->speech, text);
1704 * @brief Toggles the navigation announcer for navit
1705 * @param this_ The navit object
1708 navit_cmd_announcer_toggle(struct navit *this_)
1710 struct attr attr, speechattr;
1712 // search for the speech attribute
1713 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1715 // find out if the corresponding attribute attr_active has been set
1716 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1718 attr.u.num = !attr.u.num;
1720 // otherwise disable it because voice is enabled by default
1721 attr.type = attr_active;
1725 // apply the new state
1726 if(!speech_set_attr(speechattr.u.speech, &attr))
1729 // announce that the speech attribute has changed
1730 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1734 navit_speak(struct navit *this_)
1736 struct navigation *nav=this_->navigation;
1737 struct map *map=NULL;
1738 struct map_rect *mr=NULL;
1742 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1744 dbg(1, "this_.speech->active %i\n", attr.u.num);
1749 map=navigation_get_map(nav);
1751 mr=map_rect_new(map, NULL);
1753 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1754 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1755 speech_say(this_->speech, attr.u.str);
1756 navit_add_message(this_, attr.u.str);
1757 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1759 map_rect_destroy(mr);
1764 navit_window_roadbook_update(struct navit *this_)
1766 struct navigation *nav=this_->navigation;
1767 struct map *map=NULL;
1768 struct map_rect *mr=NULL;
1771 struct param_list param[5];
1775 datawindow_mode(this_->roadbook_window, 1);
1777 map=navigation_get_map(nav);
1779 mr=map_rect_new(map, NULL);
1780 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1782 dbg(0,"while loop\n");
1783 while ((item=map_rect_get_item(mr))) {
1784 dbg(0,"item=%p\n", item);
1786 if (item->type != type_nav_position) {
1787 item_attr_get(item, attr_navigation_long, &attr);
1788 if (attr.u.str == NULL) {
1791 dbg(2, "Command='%s'\n", attr.u.str);
1792 param[0].value=g_strdup(attr.u.str);
1794 param[0].value=_("Position");
1795 param[0].name=_("Command");
1797 item_attr_get(item, attr_length, &attr);
1798 dbg(2, "Length=%d\n", attr.u.num);
1799 param[1].name=_("Length");
1801 if ( attr.u.num >= 2000 )
1803 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1807 param[1].value=g_strdup_printf("%7ld %s",attr.u.num, _("m"));
1810 item_attr_get(item, attr_time, &attr);
1811 dbg(2, "Time=%d\n", attr.u.num);
1813 param[2].name=_("Time");
1816 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1820 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1823 item_attr_get(item, attr_destination_length, &attr);
1824 dbg(2, "Destlength=%d\n", attr.u.num);
1825 param[3].name=_("Destination Length");
1826 if ( attr.u.num >= 2000 )
1828 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1832 param[3].value=g_strdup_printf("%ld %s",attr.u.num, _("m"));
1835 item_attr_get(item, attr_destination_time, &attr);
1836 dbg(2, "Desttime=%d\n", attr.u.num);
1838 param[4].name=_("Destination Time");
1841 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1845 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1847 datawindow_add(this_->roadbook_window, param, 5);
1849 map_rect_destroy(mr);
1851 datawindow_mode(this_->roadbook_window, 0);
1855 navit_window_roadbook_destroy(struct navit *this_)
1858 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1859 callback_destroy(this_->roadbook_callback);
1860 this_->roadbook_window=NULL;
1861 this_->roadbook_callback=NULL;
1864 navit_window_roadbook_new(struct navit *this_)
1866 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1870 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1871 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1872 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1873 navit_window_roadbook_update(this_);
1877 navit_init(struct navit *this_)
1884 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1886 if (!this_->gui && !(this_->flags & 2)) {
1888 navit_destroy(this_);
1891 if (!this_->gra && !(this_->flags & 1)) {
1892 dbg(0,"no graphics\n");
1893 navit_destroy(this_);
1896 dbg(2,"Connecting gui to graphics\n");
1897 if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1898 struct attr attr_type_gui, attr_type_graphics;
1899 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1900 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1901 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1902 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1903 dbg(0," for explanations and solutions\n");
1905 navit_destroy(this_);
1908 if (this_->speech && this_->navigation) {
1910 speech.type=attr_speech;
1911 speech.u.speech=this_->speech;
1912 navigation_set_attr(this_->navigation, &speech);
1914 dbg(2,"Initializing graphics\n");
1915 dbg(2,"Setting Vehicle\n");
1916 navit_set_vehicle(this_, this_->vehicle);
1917 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1918 if (this_->mapsets) {
1919 struct mapset_handle *msh;
1920 ms=this_->mapsets->data;
1921 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1922 msh=mapset_open(ms);
1923 while (msh && (map=mapset_next(msh, 0))) {
1924 //pass new callback instance for each map in the mapset to make map callback list destruction work correctly
1925 struct callback *pcb = callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1926 map_add_callback(map, pcb);
1931 if ((map=route_get_map(this_->route))) {
1933 map_a.type=attr_map;
1935 mapset_add_attr(ms, &map_a);
1937 if ((map=route_get_graph_map(this_->route))) {
1938 struct attr map_a,active;
1939 map_a.type=attr_map;
1941 active.type=attr_active;
1943 mapset_add_attr(ms, &map_a);
1944 map_set_attr(map, &active);
1946 route_set_mapset(this_->route, ms);
1947 route_set_projection(this_->route, transform_get_projection(this_->trans));
1949 if (this_->tracking) {
1950 tracking_set_mapset(this_->tracking, ms);
1952 tracking_set_route(this_->tracking, this_->route);
1954 if (this_->navigation) {
1955 if ((map=navigation_get_map(this_->navigation))) {
1956 struct attr map_a,active;
1957 map_a.type=attr_map;
1959 active.type=attr_active;
1961 mapset_add_attr(ms, &map_a);
1962 map_set_attr(map, &active);
1965 if (this_->tracking) {
1966 if ((map=tracking_get_map(this_->tracking))) {
1967 struct attr map_a,active;
1968 map_a.type=attr_map;
1970 active.type=attr_active;
1972 mapset_add_attr(ms, &map_a);
1973 map_set_attr(map, &active);
1976 navit_add_former_destinations_from_file(this_);
1979 struct attr callback;
1980 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1981 callback.type=attr_callback;
1982 callback.u.callback=this_->route_cb;
1983 route_add_attr(this_->route, &callback);
1985 if (this_->navigation) {
1986 if (this_->speech) {
1987 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1988 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1991 navigation_set_route(this_->navigation, this_->route);
1993 dbg(2,"Setting Center\n");
1994 center_file = bookmarks_get_center_file(FALSE);
1995 bookmarks_set_center_from_file(this_->bookmarks, center_file);
1996 g_free(center_file);
1998 if (this_->menubar) {
1999 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
2001 navit_add_menu_windows_items(this_, men);
2007 navit_window_roadbook_new(this_);
2008 navit_window_items_new(this_);
2011 messagelist_init(this_->messages);
2013 navit_set_cursors(this_);
2015 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
2016 callback=(this_->ready == 2);
2018 dbg(2,"ready=%d\n",this_->ready);
2019 if (this_->ready == 3)
2020 navit_draw_async(this_, 1);
2022 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
2024 routech_test(this_);
2029 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
2034 c.x=(r->rl.x+r->lu.x)/2;
2035 c.y=(r->rl.y+r->lu.y)/2;
2036 transform_set_center(this_->trans, &c);
2037 dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
2038 while (scale < 1<<20) {
2040 transform_set_scale(this_->trans, scale);
2041 transform_setup_source_rect(this_->trans);
2042 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
2043 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
2044 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
2045 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
2046 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
2052 if (this_->ready == 3)
2053 navit_draw_async(this_,0);
2057 navit_zoom_to_route(struct navit *this_, int orientation)
2060 struct map_rect *mr=NULL;
2063 struct coord_rect r;
2068 map=route_get_map(this_->route);
2069 dbg(1,"map=%p\n",map);
2071 mr=map_rect_new(map, NULL);
2072 dbg(1,"mr=%p\n",mr);
2074 while ((item=map_rect_get_item(mr))) {
2075 dbg(1,"item=%s\n", item_to_name(item->type));
2076 while (item_coord_get(item, &c, 1)) {
2081 coord_rect_extend(&r, &c);
2085 map_rect_destroy(mr);
2089 if (orientation != -1)
2090 transform_set_yaw(this_->trans, orientation);
2091 navit_zoom_to_rect(this_, &r);
2095 navit_cmd_zoom_to_route(struct navit *this)
2097 navit_zoom_to_route(this, 0);
2102 * Change the current zoom level
2104 * @param navit The navit instance
2105 * @param center The point where to center the map, including its projection
2109 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
2111 struct coord *c=transform_center(this_->trans);
2113 enum projection pro = transform_get_projection(this_->trans);
2114 if (pro != center->pro) {
2117 transform_from_to(&c1, center->pro, &c2, pro);
2124 navit_set_timeout(this_);
2125 if (this_->ready == 3)
2130 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
2134 transform_set_center(this_->trans, c);
2135 transform_get_size(this_->trans, &width, &height);
2138 update_transformation(this_->trans, &po, p, NULL);
2140 navit_set_timeout(this_);
2144 * Links all vehicles to a cursor depending on the current profile.
2146 * @param this_ A navit instance
2147 * @author Ralph Sennhauser (10/2009)
2150 navit_set_cursors(struct navit *this_)
2153 struct navit_vehicle *nv;
2157 v=g_list_first(this_->vehicles); // GList of navit_vehicles
2160 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
2161 if (!strcmp(name.u.str,"none"))
2164 c=layout_get_cursor(this_->layout_current, name.u.str);
2166 c=layout_get_cursor(this_->layout_current, "default");
2167 vehicle_set_cursor(nv->vehicle, c, 0);
2174 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
2177 struct navit_vehicle *nv=this_->vehicle;
2179 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
2180 #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 */
2181 float min_offset = 0.; // Percent offset at min_offset_speed.
2182 float max_offset = 30.; // Percent offset at max_offset_speed.
2183 int min_offset_speed = 2; // Speed in km/h
2184 int max_offset_speed = 50; // Speed ini km/h
2185 // Calculate cursor offset from the center of the screen, upon speed.
2186 if (nv->speed <= min_offset_speed) {
2187 offset = min_offset;
2188 } else if (nv->speed > max_offset_speed) {
2189 offset = max_offset;
2191 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
2195 transform_get_size(this_->trans, &width, &height);
2196 if (this_->orientation == -1 || keep_orientation) {
2198 p->y=(50 + offset)*height/100;
2200 *dir=keep_orientation?this_->orientation:nv->dir;
2203 if (this_->tracking && this_->tracking_flag) {
2204 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
2206 mdir=nv->dir-this_->orientation;
2209 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
2210 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
2212 *dir=this_->orientation;
2218 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
2222 struct navit_vehicle *nv=this_->vehicle;
2223 navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
2224 transform_set_yaw(this_->trans, dir);
2225 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
2227 navit_autozoom(this_, &nv->coord, nv->speed, 0);
2231 navit_set_center_cursor_draw(struct navit *this_)
2233 navit_set_center_cursor(this_,1,0);
2234 if (this_->ready == 3)
2235 navit_draw_async(this_, 1);
2239 navit_cmd_set_center_cursor(struct navit *this_)
2241 navit_set_center_cursor_draw(this_);
2245 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
2249 transform_reverse(this_->trans, p, &c);
2252 pc.pro = transform_get_projection(this_->trans);
2253 navit_set_center(this_, &pc, set_timeout);
2257 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
2259 int dir=0, orient_old=0, attr_updated=0;
2263 struct navit_vehicle *nv;
2266 active.type=attr_active;
2269 switch (attr->type) {
2271 attr_updated=(this_->autozoom_secs != attr->u.num);
2272 this_->autozoom_secs = attr->u.num;
2274 case attr_autozoom_active:
2275 attr_updated=(this_->autozoom_active != attr->u.num);
2276 this_->autozoom_active = attr->u.num;
2279 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
2280 dbg(1,"0x%x,0x%x\n",co.x,co.y);
2281 transform_set_center(this_->trans, &co);
2283 case attr_drag_bitmap:
2284 attr_updated=(this_->drag_bitmap != !!attr->u.num);
2285 this_->drag_bitmap=!!attr->u.num;
2288 attr_updated=(this_->flags != attr->u.num);
2289 this_->flags=attr->u.num;
2291 case attr_flags_graphics:
2292 attr_updated=(this_->graphics_flags != attr->u.num);
2293 this_->graphics_flags=attr->u.num;
2296 if (!this_->vehicle)
2298 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
2299 this_->vehicle->follow_curr = attr->u.num;
2302 if(this_->layout_current!=attr->u.layout) {
2303 this_->layout_current=attr->u.layout;
2304 graphics_font_destroy_all(this_->gra);
2305 navit_set_cursors(this_);
2306 if (this_->ready == 3)
2311 case attr_layout_name:
2315 if (!strcmp(lay->name,attr->u.str)) {
2317 attr.type=attr_layout;
2319 return navit_set_attr_do(this_, &attr, init);
2324 case attr_map_border:
2325 if (this_->border != attr->u.num) {
2326 this_->border=attr->u.num;
2330 case attr_orientation:
2331 orient_old=this_->orientation;
2332 this_->orientation=attr->u.num;
2334 if (this_->orientation != -1) {
2335 dir = this_->orientation;
2337 if (this_->vehicle) {
2338 dir = this_->vehicle->dir;
2341 transform_set_yaw(this_->trans, dir);
2342 if (orient_old != this_->orientation) {
2344 if (this_->ready == 3)
2351 case attr_osd_configuration:
2352 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
2353 attr_updated=(this_->osd_configuration != attr->u.num);
2354 this_->osd_configuration=attr->u.num;
2357 attr_updated=(this_->pitch != attr->u.num);
2358 this_->pitch=attr->u.num;
2359 transform_set_pitch(this_->trans, this_->pitch);
2360 if (!init && attr_updated && this_->ready == 3)
2363 case attr_projection:
2364 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
2365 navit_projection_set(this_, attr->u.projection, !init);
2370 attr_updated=(this_->radius != attr->u.num);
2371 this_->radius=attr->u.num;
2373 case attr_recent_dest:
2374 attr_updated=(this_->recentdest_count != attr->u.num);
2375 this_->recentdest_count=attr->u.num;
2378 if(this_->speech && this_->speech != attr->u.speech) {
2380 this_->speech = attr->u.speech;
2384 attr_updated=(this_->center_timeout != attr->u.num);
2385 this_->center_timeout = attr->u.num;
2388 attr_updated=(this_->tracking_flag != !!attr->u.num);
2389 this_->tracking_flag=!!attr->u.num;
2391 case attr_transformation:
2392 this_->trans=attr->u.transformation;
2394 case attr_use_mousewheel:
2395 attr_updated=(this_->use_mousewheel != !!attr->u.num);
2396 this_->use_mousewheel=!!attr->u.num;
2402 if (nv->vehicle == attr->u.vehicle) {
2403 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2405 vehicle_set_attr(this_->vehicle->vehicle, &active);
2407 vehicle_set_attr(nv->vehicle, &active);
2410 navit_set_vehicle(this_, nv);
2416 zoom=transform_get_scale(this_->trans);
2417 attr_updated=(zoom != attr->u.num);
2418 transform_set_scale(this_->trans, attr->u.num);
2419 if (attr_updated && !init)
2423 attr_updated=(attr->u.num != this_->zoom_min);
2424 this_->zoom_min=attr->u.num;
2427 attr_updated=(attr->u.num != this_->zoom_max);
2428 this_->zoom_max=attr->u.num;
2431 navit_add_message(this_, attr->u.str);
2433 case attr_follow_cursor:
2434 attr_updated=(this_->follow_cursor != !!attr->u.num);
2435 this_->follow_cursor=!!attr->u.num;
2438 attr_updated=(this_->imperial != attr->u.num);
2439 this_->imperial=attr->u.num;
2444 if (attr_updated && !init) {
2445 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2446 if (attr->type == attr_osd_configuration)
2447 graphics_draw_mode(this_->gra, draw_mode_end);
2453 navit_set_attr(struct navit *this_, struct attr *attr)
2455 return navit_set_attr_do(this_, attr, 0);
2459 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2461 struct message *msg;
2467 msg = navit_get_messages(this_);
2475 len += strlen(msg->text) + 1;
2478 attr->u.str = g_malloc(len + 1);
2480 msg = navit_get_messages(this_);
2483 g_stpcpy((attr->u.str + offset), msg->text);
2484 offset += strlen(msg->text);
2485 attr->u.str[offset] = '\n';
2491 attr->u.str[len] = '\0';
2494 attr->u.num=this_->imperial;
2496 case attr_bookmark_map:
2497 attr->u.map=bookmarks_get_map(this_->bookmarks);
2499 case attr_bookmarks:
2500 attr->u.bookmarks=this_->bookmarks;
2502 case attr_callback_list:
2503 attr->u.callback_list=this_->attr_cbl;
2505 case attr_destination:
2506 if (! this_->destination_valid)
2508 attr->u.pcoord=&this_->destination;
2510 case attr_displaylist:
2511 attr->u.displaylist=this_->displaylist;
2512 return (attr->u.displaylist != NULL);
2514 if (!this_->vehicle)
2516 attr->u.num=this_->vehicle->follow_curr;
2518 case attr_former_destination_map:
2519 attr->u.map=this_->former_destination;
2522 attr->u.graphics=this_->gra;
2523 ret=(attr->u.graphics != NULL);
2526 attr->u.gui=this_->gui;
2527 ret=(attr->u.gui != NULL);
2532 iter->u.list=g_list_next(iter->u.list);
2534 iter->u.list=this_->layouts;
2538 attr->u.layout=(struct layout *)iter->u.list->data;
2540 attr->u.layout=this_->layout_current;
2544 if (iter && this_->mapsets) {
2545 if (!iter->u.mapset_handle) {
2546 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2548 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2550 mapset_close(iter->u.mapset_handle);
2558 attr->u.mapset=this_->mapsets->data;
2559 ret=(attr->u.mapset != NULL);
2561 case attr_navigation:
2562 attr->u.navigation=this_->navigation;
2564 case attr_orientation:
2565 attr->u.num=this_->orientation;
2568 ret=attr_generic_get_attr(this_->attr_list, NULL, type, attr, iter?(struct attr_iter *)&iter->iter:NULL);
2570 case attr_osd_configuration:
2571 attr->u.num=this_->osd_configuration;
2574 attr->u.num=transform_get_pitch(this_->trans);
2576 case attr_projection:
2578 attr->u.num=transform_get_projection(this_->trans);
2584 attr->u.route=this_->route;
2587 attr->u.speech=this_->speech;
2590 attr->u.num=this_->tracking_flag;
2592 case attr_trackingo:
2593 attr->u.tracking=this_->tracking;
2595 case attr_transformation:
2596 attr->u.transformation=this_->trans;
2601 iter->u.list=g_list_next(iter->u.list);
2603 iter->u.list=this_->vehicles;
2607 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2609 if(this_->vehicle) {
2610 attr->u.vehicle=this_->vehicle->vehicle;
2616 case attr_vehicleprofile:
2617 attr->u.vehicleprofile=this_->vehicleprofile;
2620 attr->u.num=transform_get_scale(this_->trans);
2622 case attr_autozoom_active:
2623 attr->u.num=this_->autozoom_active;
2625 case attr_follow_cursor:
2626 attr->u.num=this_->follow_cursor;
2636 navit_add_log(struct navit *this_, struct log *log)
2638 struct attr type_attr;
2639 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2641 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2642 char *header = "type=track_tracked\n";
2643 if (this_->textfile_debug_log)
2645 log_set_header(log, header, strlen(header));
2646 this_->textfile_debug_log=log;
2653 navit_add_layout(struct navit *this_, struct layout *layout)
2656 this_->layouts = g_list_append(this_->layouts, layout);
2657 layout_get_attr(layout, attr_active, &active, NULL);
2658 if(active.u.num || !this_->layout_current) {
2659 this_->layout_current=layout;
2666 navit_add_attr(struct navit *this_, struct attr *attr)
2669 switch (attr->type) {
2671 navit_add_callback(this_, attr->u.callback);
2674 ret=navit_add_log(this_, attr->u.log);
2677 ret=navit_set_gui(this_, attr->u.gui);
2680 ret=navit_set_graphics(this_, attr->u.graphics);
2683 ret=navit_add_layout(this_, attr->u.layout);
2686 this_->route=attr->u.route;
2689 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2691 case attr_navigation:
2692 this_->navigation=attr->u.navigation;
2695 this_->attr_list=attr_generic_add_attr(this_->attr_list, attr);
2697 case attr_recent_dest:
2698 this_->recentdest_count = attr->u.num;
2701 this_->speech=attr->u.speech;
2704 this_->tracking=attr->u.tracking;
2707 ret=navit_add_vehicle(this_, attr->u.vehicle);
2709 case attr_vehicleprofile:
2710 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2712 case attr_autozoom_min:
2713 this_->autozoom_min = attr->u.num;
2718 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2723 navit_remove_attr(struct navit *this_, struct attr *attr)
2726 switch (attr->type) {
2728 navit_remove_callback(this_, attr->u.callback);
2737 navit_attr_iter_new(void)
2739 return g_new0(struct attr_iter, 1);
2743 navit_attr_iter_destroy(struct attr_iter *iter)
2749 navit_add_callback(struct navit *this_, struct callback *cb)
2751 callback_list_add(this_->attr_cbl, cb);
2755 navit_remove_callback(struct navit *this_, struct callback *cb)
2757 callback_list_remove(this_->attr_cbl, cb);
2761 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2763 * @param navit The navit instance
2768 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2770 struct point cursor_pnt;
2771 enum projection pro;
2778 pro=transform_get_projection(this_->trans_cursor);
2781 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2783 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2788 pro=transform_get_projection(this_->trans);
2789 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2792 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2794 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2800 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2802 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2803 struct pcoord cursor_pc;
2804 struct point cursor_pnt, *pnt=&cursor_pnt;
2805 struct tracking *tracking=NULL;
2806 struct pcoord pc[16];
2807 enum projection pro=transform_get_projection(this_->trans_cursor);
2809 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2811 char *destination_file;
2814 if (this_->ready != 3) {
2815 profile(0,"return 1\n");
2818 navit_layout_switch(this_);
2819 if (this_->vehicle == nv && this_->tracking_flag)
2820 tracking=this_->tracking;
2822 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2823 attr_object=tracking;
2824 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2826 attr_object=nv->vehicle;
2827 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2829 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2830 if (!attr_valid.u.num != attr_position_valid_invalid)
2832 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2833 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2834 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2835 profile(0,"return 2\n");
2838 nv->dir=*attr_dir.u.numd;
2839 nv->speed=*attr_speed.u.numd;
2840 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2841 if (nv != this_->vehicle) {
2842 navit_vehicle_draw(this_, nv, NULL);
2843 profile(0,"return 3\n");
2846 cursor_pc.x = nv->coord.x;
2847 cursor_pc.y = nv->coord.y;
2848 cursor_pc.pro = pro;
2851 route_set_position_from_tracking(this_->route, tracking, pro);
2853 route_set_position(this_->route, &cursor_pc);
2855 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2856 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2857 if (this_->gui && nv->speed > 2)
2858 navit_disable_suspend();
2860 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2861 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2862 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2863 navit_set_center_cursor_draw(this_);
2865 navit_vehicle_draw(this_, nv, pnt);
2867 if (nv->follow_curr > 1)
2870 nv->follow_curr=nv->follow;
2871 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2873 /* Finally, if we reached our destination, stop navigation. */
2875 switch(route_destination_reached(this_->route)) {
2877 route_remove_waypoint(this_->route);
2878 count=route_get_destinations(this_->route, pc, 16);
2879 destination_file = bookmarks_get_destination_file(TRUE);
2880 bookmarks_append_coord(this_->former_destination, destination_file, pc, type_former_itinerary_part, NULL, this_->recentdest_count);
2883 navit_set_destination(this_, NULL, NULL, 0);
2887 profile(0,"return 5\n");
2891 * Set the position of the vehicle
2893 * @param navit The navit instance
2894 * @param c The coordinate to set as position
2899 navit_set_position(struct navit *this_, struct pcoord *c)
2902 route_set_position(this_->route, c);
2903 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2905 if (this_->ready == 3)
2910 navit_set_vehicleprofile(struct navit *this_, char *name)
2914 l=this_->vehicleprofiles;
2916 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2917 if (!strcmp(attr.u.str, name)) {
2918 this_->vehicleprofile=l->data;
2920 route_set_profile(this_->route, this_->vehicleprofile);
2930 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2934 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2935 if (navit_set_vehicleprofile(this_, attr.u.str))
2938 if (!navit_set_vehicleprofile(this_,"car")) {
2939 /* We do not have a fallback "car" profile
2940 * so lets set any profile */
2942 l=this_->vehicleprofiles;
2944 this_->vehicleprofile=l->data;
2946 route_set_profile(this_->route, this_->vehicleprofile);
2952 * Register a new vehicle
2954 * @param navit The navit instance
2955 * @param v The vehicle instance
2956 * @returns 1 for success
2959 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2961 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2962 struct attr follow, active, animate;
2967 nv->animate_cursor=0;
2968 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2969 nv->follow=follow.u.num;
2970 nv->follow_curr=nv->follow;
2971 this_->vehicles=g_list_append(this_->vehicles, nv);
2972 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2973 navit_set_vehicle(this_, nv);
2974 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2975 nv->animate_cursor=animate.u.num;
2976 nv->callback.type=attr_callback;
2977 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2978 vehicle_add_attr(nv->vehicle, &nv->callback);
2979 vehicle_set_attr(nv->vehicle, &this_->self);
2987 navit_get_gui(struct navit *this_)
2992 struct transformation *
2993 navit_get_trans(struct navit *this_)
2995 return this_->trans;
2999 navit_get_route(struct navit *this_)
3001 return this_->route;
3005 navit_get_navigation(struct navit *this_)
3007 return this_->navigation;
3010 struct displaylist *
3011 navit_get_displaylist(struct navit *this_)
3013 return this_->displaylist;
3017 navit_layout_switch(struct navit *n)
3021 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
3022 double trise,tset,trise_actual;
3024 int year, month, day;
3026 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
3027 return; //No layout - nothing to switch
3031 l=layout_attr.u.layout;
3033 if (l->dayname || l->nightname) {
3034 //Ok, we know that we have profile to switch
3036 //Check that we aren't calculating too fast
3037 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
3038 currTs=iso8601_to_secs(iso8601_attr.u.str);
3039 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
3041 if (currTs-(n->prevTs)<60) {
3042 //We've have to wait a little
3045 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
3047 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
3048 return; //No valid fix yet
3050 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
3051 //No position - no sun
3055 //We calculate sunrise anyway, cause it is needed both for day and for night
3056 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3057 //near the pole sun never rises/sets, so we should never switch profiles
3058 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
3064 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
3067 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
3068 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
3069 //The sun is rising now!
3070 if (strcmp(l->name,l->dayname)) {
3071 navit_set_layout_by_name(n,l->dayname);
3076 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3077 //near the pole sun never rises/sets, so we should never switch profiles
3078 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
3082 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
3083 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
3084 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
3085 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
3087 if (strcmp(l->name,l->nightname)) {
3088 navit_set_layout_by_name(n,l->nightname);
3098 navit_set_vehicle_by_name(struct navit *n,const char *name)
3101 struct attr_iter *iter;
3102 struct attr vehicle_attr, name_attr;
3104 iter=navit_attr_iter_new();
3106 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
3107 v=vehicle_attr.u.vehicle;
3108 vehicle_get_attr(v,attr_name,&name_attr,NULL);
3109 if (name_attr.type==attr_name) {
3110 if (!strcmp(name,name_attr.u.str)) {
3111 navit_set_attr(n,&vehicle_attr);
3112 navit_attr_iter_destroy(iter);
3117 navit_attr_iter_destroy(iter);
3122 navit_set_layout_by_name(struct navit *n,const char *name)
3125 struct attr_iter iter;
3126 struct attr layout_attr;
3130 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
3131 return 0; //No layouts - nothing to do
3133 if (iter.u.list==NULL) {
3137 iter.u.list=g_list_first(iter.u.list);
3139 while(iter.u.list) {
3140 l=(struct layout*)iter.u.list->data;
3141 if (!strcmp(name,l->name)) {
3142 layout_attr.u.layout=l;
3143 layout_attr.type=attr_layout;
3144 navit_set_attr(n,&layout_attr);
3145 iter.u.list=g_list_first(iter.u.list);
3148 iter.u.list=g_list_next(iter.u.list);
3151 iter.u.list=g_list_first(iter.u.list);
3156 navit_disable_suspend() {
3157 gui_disable_suspend(global_navit->gui);
3158 callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
3162 navit_block(struct navit *this_, int block)
3165 this_->blocked |= 1;
3166 if (graphics_draw_cancel(this_->gra, this_->displaylist))
3167 this_->blocked |= 2;
3170 if ((this_->blocked & 2) || block < 0) {
3180 navit_destroy(struct navit *this_)
3183 callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
3185 /* TODO: destroy objects contained in this_ */
3187 vehicle_destroy(this_->vehicle->vehicle);
3189 if (this_->bookmarks) {
3190 char *center_file = bookmarks_get_center_file(TRUE);
3191 bookmarks_write_center_to_file(this_->bookmarks, center_file);
3192 g_free(center_file);
3193 bookmarks_destroy(this_->bookmarks);
3196 callback_destroy(this_->nav_speech_cb);
3197 callback_destroy(this_->roadbook_callback);
3198 callback_destroy(this_->popup_callback);
3199 callback_destroy(this_->motion_timeout_callback);
3200 callback_destroy(this_->progress_cb);
3203 graphics_remove_callback(this_->gra, this_->resize_callback);
3204 graphics_remove_callback(this_->gra, this_->button_callback);
3205 graphics_remove_callback(this_->gra, this_->motion_callback);
3206 graphics_remove_callback(this_->gra, this_->predraw_callback);
3209 callback_destroy(this_->resize_callback);
3210 callback_destroy(this_->motion_callback);
3211 callback_destroy(this_->predraw_callback);
3213 callback_destroy(this_->route_cb);
3214 route_destroy(this_->route);
3216 map_destroy(this_->former_destination);
3218 ms = navit_get_mapset(this_);
3222 graphics_free(this_->gra);