2 * Navit, a modular navigation system.
3 * Copyright (C) 2005-2009 Navit Team
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * version 2 as published by the Free Software Foundation.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the
16 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301, USA.
20 #define _USE_MATH_DEFINES 1
38 #include "projection.h"
44 #include "transform.h"
49 #include "data_window.h"
51 #include "navigation.h"
62 #include "navit_nls.h"
66 #include "vehicleprofile.h"
68 #include "bookmarks.h"
69 #ifdef HAVE_API_WIN32_BASE
75 * @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
79 //! The navit_vehicule
80 struct navit_vehicle {
82 /*! Limit of the follow counter. See navit_add_vehicle */
84 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
88 struct coord last; /*< Position of the last update of this vehicle */
89 struct vehicle *vehicle;
99 struct layout *layout_current;
100 struct graphics *gra;
101 struct action *action;
102 struct transformation *trans, *trans_cursor;
103 struct compass *compass;
105 struct navigation *navigation;
106 struct speech *speech;
107 struct tracking *tracking;
110 struct displaylist *displaylist;
113 int recentdest_count;
114 int osd_configuration;
116 GList *windows_items;
117 struct navit_vehicle *vehicle;
118 struct callback_list *attr_cbl;
119 struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb, *progress_cb;
120 struct datawindow *roadbook_window;
121 struct map *former_destination;
122 struct point pressed, last, current;
123 int button_pressed,moved,popped,zoomed;
128 struct event_timeout *button_timeout, *motion_timeout;
129 struct callback *motion_timeout_callback;
131 int ignore_graphics_events;
132 struct log *textfile_debug_log;
133 struct pcoord destination;
134 int destination_valid;
139 struct messagelist *messages;
140 struct callback *resize_callback,*button_callback,*motion_callback,*predraw_callback;
141 struct vehicleprofile *vehicleprofile;
142 GList *vehicleprofiles;
147 int zoom_min, zoom_max;
149 struct bookmarks *bookmarks;
151 /* 1=No graphics ok */
157 struct gui *main_loop_gui;
162 struct mapset_handle *mapset_handle;
166 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
167 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
168 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
169 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
170 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
171 static void navit_set_cursors(struct navit *this_);
172 static void navit_cmd_zoom_to_route(struct navit *this);
173 static void navit_cmd_set_center_cursor(struct navit *this_);
174 static void navit_cmd_announcer_toggle(struct navit *this_);
175 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
177 struct navit *global_navit;
180 navit_add_mapset(struct navit *this_, struct mapset *ms)
182 this_->mapsets = g_list_append(this_->mapsets, ms);
186 navit_get_mapset(struct navit *this_)
189 return this_->mapsets->data;
191 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
197 navit_get_tracking(struct navit *this_)
199 return this_->tracking;
203 * @brief Get the user data directory.
204 * @param[in] create - create the directory if it does not exist
206 * @return char * to the data directory string.
208 * returns the directory used to store user data files (center.txt,
209 * destination.txt, bookmark.txt, ...)
213 navit_get_user_data_directory(int create) {
215 dir = getenv("NAVIT_USER_DATADIR");
216 if (create && !file_exists(dir)) {
217 dbg(0,"creating dir %s\n", dir);
218 if (file_mkdir(dir,0)) {
219 dbg(0,"failed creating dir %s\n", dir);
224 } /* end: navit_get_user_data_directory(gboolean create) */
228 navit_draw_async(struct navit *this_, int async)
231 if (this_->blocked) {
235 transform_setup_source_rect(this_->trans);
236 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
240 navit_draw(struct navit *this_)
242 if (this_->ready == 3)
243 navit_draw_async(this_, 0);
247 navit_get_ready(struct navit *this_)
255 navit_draw_displaylist(struct navit *this_)
257 if (this_->ready == 3)
258 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
262 navit_map_progress(struct navit *this_)
266 struct mapset_handle *msh;
269 if (this_->ready != 3)
274 ms=this_->mapsets->data;
276 while (msh && (map=mapset_next(msh, 0))) {
277 if (map_get_attr(map, attr_progress, &attr, NULL)) {
278 char *str=g_strdup_printf("%s ",attr.u.str);
279 graphics_draw_mode(this_->gra, draw_mode_begin);
280 graphics_draw_text_std(this_->gra, 16, str, &p);
283 graphics_draw_mode(this_->gra, draw_mode_end);
290 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
293 if (attr->type != attr_route_status)
296 if (this_->ready != 3)
298 if (updated != route_status_path_done_new)
300 if (this_->vehicle) {
301 if (this_->vehicle->follow_curr == 1)
303 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
304 this_->vehicle->follow_curr=this_->vehicle->follow;
310 navit_handle_resize(struct navit *this_, int w, int h)
312 struct map_selection sel;
313 int callback=(this_->ready == 1);
315 memset(&sel, 0, sizeof(sel));
320 transform_set_screen_selection(this_->trans, &sel);
321 graphics_init(this_->gra);
322 graphics_set_rect(this_->gra, &sel.u.p_rect);
324 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
325 if (this_->ready == 3)
326 navit_draw_async(this_, 1);
330 navit_resize(void *data, int w, int h)
332 struct navit *this=data;
333 if (!this->ignore_graphics_events)
334 navit_handle_resize(this, w, h);
338 navit_get_width(struct navit *this_)
345 navit_get_height(struct navit *this_)
351 navit_popup(void *data)
353 struct navit *this_=data;
354 popup(this_, 1, &this_->pressed);
355 this_->button_timeout=NULL;
361 navit_ignore_button(struct navit *this_)
363 if (this_->ignore_button)
365 this_->ignore_button=1;
370 navit_ignore_graphics_events(struct navit *this_, int ignore)
372 this_->ignore_graphics_events=ignore;
376 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
381 double angleo,anglen;
383 if (!transform_reverse(tr, old, &co))
386 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
387 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
388 yaw=transform_get_yaw(tr)+angleo-anglen;
389 transform_set_yaw(tr, yaw % 360);
391 if (!transform_reverse(tr, new, &cn))
393 cp=transform_get_center(tr);
396 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
397 transform_set_center(tr, &c);
401 navit_set_timeout(struct navit *this_)
404 follow.type=attr_follow;
405 follow.u.num=this_->center_timeout;
406 navit_set_attr(this_, &follow);
410 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
414 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
415 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
416 if (this_->ignore_button) {
417 this_->ignore_button=0;
425 this_->button_pressed=1;
429 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
432 navit_set_center_screen(this_, p, 1);
434 popup(this_, button, p);
435 if (button == 4 && this_->use_mousewheel) {
437 navit_zoom_in(this_, 2, p);
439 if (button == 5 && this_->use_mousewheel) {
441 navit_zoom_out(this_, 2, p);
445 this_->button_pressed=0;
446 if (this_->button_timeout) {
447 event_remove_timeout(this_->button_timeout);
448 this_->button_timeout=NULL;
449 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
450 navit_set_center_screen(this_, p, !this_->zoomed);
453 if (this_->motion_timeout) {
454 event_remove_timeout(this_->motion_timeout);
455 this_->motion_timeout=NULL;
462 update_transformation(this_->trans, &this_->pressed, p, &pr);
464 update_transformation(this_->trans, &this_->pressed, p, NULL);
466 graphics_draw_drag(this_->gra, NULL);
467 transform_copy(this_->trans, this_->trans_cursor);
468 graphics_overlay_disable(this_->gra, 0);
470 navit_set_timeout(this_);
479 navit_button(void *data, int pressed, int button, struct point *p)
481 struct navit *this=data;
482 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
483 if (!this->ignore_graphics_events) {
484 if (! this->popup_callback)
485 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
486 navit_handle_button(this, pressed, button, p, this->popup_callback);
492 navit_motion_timeout(struct navit *this_)
496 if (this_->drag_bitmap) {
498 point.x=(this_->current.x-this_->pressed.x);
499 point.y=(this_->current.y-this_->pressed.y);
500 if (graphics_draw_drag(this_->gra, &point)) {
501 graphics_overlay_disable(this_->gra, 1);
502 graphics_draw_mode(this_->gra, draw_mode_end);
504 this_->motion_timeout=NULL;
508 dx=(this_->current.x-this_->last.x);
509 dy=(this_->current.y-this_->last.y);
511 struct transformation *tr;
513 this_->last=this_->current;
514 graphics_overlay_disable(this_->gra, 1);
515 tr=transform_dup(this_->trans);
519 update_transformation(tr, &this_->pressed, &this_->current, &pr);
521 update_transformation(tr, &this_->pressed, &this_->current, NULL);
524 graphics_displaylist_move(this_->displaylist, dx, dy);
526 graphics_draw_cancel(this_->gra, this_->displaylist);
527 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
528 transform_destroy(tr);
531 this_->motion_timeout=NULL;
536 navit_handle_motion(struct navit *this_, struct point *p)
540 if (this_->button_pressed && !this_->popped) {
541 dx=(p->x-this_->pressed.x);
542 dy=(p->y-this_->pressed.y);
543 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
545 if (this_->button_timeout) {
546 event_remove_timeout(this_->button_timeout);
547 this_->button_timeout=NULL;
550 if (! this_->motion_timeout_callback)
551 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
552 if (! this_->motion_timeout)
553 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
559 navit_motion(void *data, struct point *p)
561 struct navit *this=data;
562 if (!this->ignore_graphics_events)
563 navit_handle_motion(this, p);
567 navit_predraw(struct navit *this_)
570 struct navit_vehicle *nv;
571 transform_copy(this_->trans, this_->trans_cursor);
575 navit_vehicle_draw(this_, nv, NULL);
581 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
583 struct coord c1, c2, *center;
584 if (scale < this_->zoom_min)
585 scale=this_->zoom_min;
586 if (scale > this_->zoom_max)
587 scale=this_->zoom_max;
589 transform_reverse(this_->trans, p, &c1);
590 transform_set_scale(this_->trans, scale);
592 transform_reverse(this_->trans, p, &c2);
593 center = transform_center(this_->trans);
594 center->x += c1.x - c2.x;
595 center->y += c1.y - c2.y;
602 * @brief Automatically adjusts zoom level
604 * This function automatically adjusts the current
605 * zoom level according to the current speed.
607 * @param this_ The navit struct
608 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
609 * @param speed The vehicles speed in meters per second
610 * @param dir The direction into which the vehicle moves
613 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
620 if (! this_->autozoom_active) {
624 distance = speed * this_->autozoom_secs;
626 transform_get_size(this_->trans, &w, &h);
627 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
628 scale = transform_get_scale(this_->trans);
630 /* We make sure that the point we want to see is within a certain range
631 * around the vehicle. The radius of this circle is the size of the
632 * screen. This doesn't necessarily mean the point is visible because of
633 * perspective etc. Quite rough, but should be enough. */
636 new_scale = (double)distance / h * 16;
638 new_scale = (double)distance / w * 16;
641 if (abs(new_scale - scale) < 2) {
645 if (new_scale >= this_->autozoom_min) {
646 navit_scale(this_, (long)new_scale, &pc, 0);
648 if (scale != this_->autozoom_min) {
649 navit_scale(this_, this_->autozoom_min, &pc, 0);
655 * Change the current zoom level, zooming closer to the ground
657 * @param navit The navit instance
658 * @param factor The zoom factor, usually 2
659 * @param p The invariant point (if set to NULL, default to center)
663 navit_zoom_in(struct navit *this_, int factor, struct point *p)
665 long scale=transform_get_scale(this_->trans)/factor;
668 navit_scale(this_, scale, p, 1);
672 * Change the current zoom level
674 * @param navit The navit instance
675 * @param factor The zoom factor, usually 2
676 * @param p The invariant point (if set to NULL, default to center)
680 navit_zoom_out(struct navit *this_, int factor, struct point *p)
682 long scale=transform_get_scale(this_->trans)*factor;
683 navit_scale(this_, scale, p, 1);
687 navit_zoom_in_cursor(struct navit *this_, int factor)
690 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
691 navit_zoom_in(this_, factor, &p);
692 this_->vehicle->follow_curr=this_->vehicle->follow;
694 navit_zoom_in(this_, factor, NULL);
698 navit_zoom_out_cursor(struct navit *this_, int factor)
701 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
702 navit_zoom_out(this_, 2, &p);
703 this_->vehicle->follow_curr=this_->vehicle->follow;
705 navit_zoom_out(this_, 2, NULL);
709 navit_cmd_zoom_in(struct navit *this_)
711 navit_zoom_in_cursor(this_, 2);
716 navit_cmd_zoom_out(struct navit *this_)
718 navit_zoom_out_cursor(this_, 2);
724 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
726 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
727 navit_say(this, in[0]->u.str);
730 static GHashTable *cmd_int_var_hash = NULL;
731 static GHashTable *cmd_attr_var_hash = NULL;
734 * Store key value pair for the command system (for int typed values)
736 * @param navit The navit instance
737 * @param function unused (needed to match command function signiture)
738 * @param in input attributes in[0] is the key string, in[1] is the integer value to store
739 * @param out output attributes, unused
740 * @param valid unused
744 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
748 if(!cmd_int_var_hash) {
749 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
752 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
753 (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
754 val = g_new(struct attr,1);
755 attr_dup_content(in[1],val);
756 key = g_strdup(in[0]->u.str);
757 g_hash_table_insert(cmd_int_var_hash, key, val);
763 * Store key value pair for the command system (for attr typed values, can be used as opaque handles)
765 * @param navit The navit instance
766 * @param function unused (needed to match command function signiture)
767 * @param in input attributes in[0] is the key string, in[1] is the attr* value to store
768 * @param out output attributes, unused
769 * @param valid unused
772 //TODO free stored attributes on navit_destroy
774 navit_cmd_set_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
778 if(!cmd_attr_var_hash) {
779 cmd_attr_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
782 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
784 val = attr_dup(in[1]);
786 key = g_strdup(in[0]->u.str);
787 g_hash_table_insert(cmd_attr_var_hash, key, val);
794 * command to toggle the active state of a named layer of the current layout
796 * @param navit The navit instance
797 * @param function unused (needed to match command function signiture)
798 * @param in input attribute in[0] is the name of the layer
799 * @param out output unused
800 * @param valid unused
804 navit_cmd_toggle_layer(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
806 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
807 if(this->layout_current && this->layout_current->layers) {
808 GList* layers = this->layout_current->layers;
810 struct layer*l=layers->data;
811 if(l && !strcmp(l->name,in[0]->u.str) ) {
816 layers=g_list_next(layers);
823 * adds an item with the current coordinate of the vehicle to a named map
825 * @param navit The navit instance
826 * @param function unused (needed to match command function signiture)
827 * @param in input attribute in[0] is the name of the map
828 * @param out output attribute, 0 on error or the id of the created item on success
829 * @param valid unused
833 navit_cmd_map_add_curr_pos(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
835 struct attr **list = g_new0(struct attr *,2);
836 struct attr*val = g_new0(struct attr,1);
838 struct map_selection sel;
839 const int selection_range = 10;
840 enum item_type item_type;
842 struct map* curr_map = NULL;
843 struct coord curr_coord;
846 val->type = attr_type_item_begin;
847 val->u.item = NULL; //return invalid item on error
852 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str && //map name
853 in[1] && ATTR_IS_STRING(in[1]->type) && in[1]->u.str //item type
856 if(!(ms=navit_get_mapset(this))) {
860 if((item_type = item_from_name(in[1]->u.str))==type_none) {
864 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
866 //no map with the given name found
871 if(this->vehicle && this->vehicle->vehicle ) {
872 struct attr pos_attr;
873 if(vehicle_get_attr(this->vehicle->vehicle,attr_position_coord_geo,&pos_attr,NULL)) {
874 transform_from_geo(projection_mg, pos_attr.u.coord_geo, &curr_coord);
884 sel.range.min=type_none;
885 sel.range.max=type_tec_common;
886 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
887 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
888 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
889 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
891 mr = map_rect_new(curr_map, &sel);
893 it = map_rect_create_item( mr, item_type);
894 item_coord_set(it,&curr_coord, 1, change_mode_modify);
897 map_rect_destroy(mr);
902 * sets an attribute (name value pair) of a map item specified by map name and item id
904 * @param navit The navit instance
905 * @param function unused (needed to match command function signiture)
906 * @param in input attribute in[0] - name of the map ; in[1] - item ; in[2] - attr name ; in[3] - attr value
907 * @param out output attribute, 0 on error, 1 on success
908 * @param valid unused
912 navit_cmd_map_item_set_attr(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
915 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str &&//map name
916 in[1] && ATTR_IS_ITEM(in[1]->type) && //item
917 in[2] && ATTR_IS_STRING(in[2]->type) && in[2]->u.str && //attr_type str
918 in[3] && ATTR_IS_STRING(in[3]->type) && in[3]->u.str //attr_value str
920 struct attr attr_to_set;
921 struct map* curr_map = NULL;
923 struct map_selection sel;
924 const int selection_range = 500;
925 struct coord curr_coord;
928 if(ATTR_IS_STRING(attr_from_name(in[2]->u.str))) {
929 attr_to_set.u.str = in[3]->u.str;
930 attr_to_set.type = attr_from_name(in[2]->u.str);
932 else if(ATTR_IS_INT(attr_from_name(in[2]->u.str))) {
933 attr_to_set.u.num = atoi(in[3]->u.str);
934 attr_to_set.type = attr_from_name(in[2]->u.str);
936 else if(ATTR_IS_DOUBLE(attr_from_name(in[2]->u.str))) {
937 double* val = g_new0(double,1);
938 *val = atof(in[3]->u.str);
939 attr_to_set.u.numd = val;
940 attr_to_set.type = attr_from_name(in[2]->u.str);
943 ms = navit_get_mapset(this);
945 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
952 sel.range.min=type_none;
953 sel.range.max=type_tec_common;
954 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
955 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
956 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
957 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
961 item_attr_set(it, &attr_to_set, change_mode_modify);
967 * Get attr variable given a key string for the command system (for opaque usage)
969 * @param navit The navit instance
970 * @param function unused (needed to match command function signiture)
971 * @param in input attribute in[0] is the key string
972 * @param out output attribute, the attr for the given key string if exists or NULL
973 * @param valid unused
977 navit_cmd_get_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
979 struct attr **list = g_new0(struct attr *,2);
980 if(!cmd_int_var_hash) {
981 struct attr*val = g_new0(struct attr,1);
982 val->type = attr_type_item_begin;
986 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
987 struct attr*ret = g_hash_table_lookup(cmd_attr_var_hash, in[0]->u.str);
989 list[0] = attr_dup(ret);
992 struct attr*val = g_new0(struct attr,1);
993 val->type = attr_type_int_begin;
1004 * Get value given a key string for the command system
1006 * @param navit The navit instance
1007 * @param function unused (needed to match command function signiture)
1008 * @param in input attribute in[0] is the key string
1009 * @param out output attribute, the value for the given key string if exists or 0
1010 * @param valid unused
1014 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1016 struct attr **list = g_new0(struct attr *,2);
1017 if(!cmd_int_var_hash) {
1018 struct attr*val = g_new0(struct attr,1);
1019 val->type = attr_type_int_begin;
1023 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
1024 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
1029 struct attr*val = g_new0(struct attr,1);
1030 val->type = attr_type_int_begin;
1039 GList *cmd_int_var_stack = NULL;
1042 * Push an integer to the stack for the command system
1044 * @param navit The navit instance
1045 * @param function unused (needed to match command function signiture)
1046 * @param in input attribute in[0] is the integer attibute to push
1047 * @param out output attributes, unused
1048 * @param valid unused
1052 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1054 if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
1055 struct attr*val = g_new(struct attr,1);
1056 attr_dup_content(in[0],val);
1057 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
1062 * Pop an integer from the command system's integer stack
1064 * @param navit The navit instance
1065 * @param function unused (needed to match command function signiture)
1066 * @param in input attributes unused
1067 * @param out output attribute, the value popped if stack isn't empty or 0
1068 * @param valid unused
1072 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1074 struct attr **list = g_new0(struct attr *,2);
1075 if(!cmd_int_var_stack) {
1076 struct attr*val = g_new0(struct attr,1);
1077 val->type = attr_type_int_begin;
1082 list[0] = cmd_int_var_stack->data;
1083 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1090 * Get current size of 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 size of stack
1096 * @param valid unused
1100 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1103 struct attr *attr = g_new0(struct attr ,1);
1104 attr->type = attr_type_int_begin;
1105 if(!cmd_int_var_stack) {
1109 attr->u.num = g_list_length(cmd_int_var_stack);
1111 list = g_new0(struct attr *,2);
1115 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1119 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1122 char *description=NULL;
1127 pc.pro = transform_get_projection(this->trans);
1128 if (ATTR_IS_COORD(in[0]->type)) {
1129 pc.x=in[0]->u.coord->x;
1130 pc.y=in[0]->u.coord->y;
1132 } else if (ATTR_IS_PCOORD(in[0]->type)) {
1133 pc=*in[0]->u.pcoord;
1135 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
1136 pc.pro=in[0]->u.num;
1140 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
1146 if (in[0] && ATTR_IS_STRING(in[0]->type))
1147 description=in[0]->u.str;
1148 navit_set_destination(this, &pc, description, 1);
1152 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1155 attr.type=attr_type_string_begin;
1156 attr.u.str="Fix me";
1158 *out=attr_generic_add_attr(*out, &attr);
1163 * Join several string attributes into one
1165 * @param navit The navit instance
1166 * @param function unused (needed to match command function signiture)
1167 * @param in input attributes in[0] - separator, in[1..] - attributes to join
1168 * @param out output attribute joined attribute as string
1169 * @param valid unused
1173 navit_cmd_strjoin(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1178 attr.type=attr_type_string_begin;
1180 if(in[0] && in[1]) {
1181 sep=attr_to_text(in[0],NULL,1);
1182 ret=attr_to_text(in[1],NULL,1);
1183 for(i=2;in[i];i++) {
1184 gchar *in_i=attr_to_text(in[i],NULL,1);
1185 gchar *r=g_strjoin(sep,ret,in_i,NULL);
1193 *out=attr_generic_add_attr(*out, &attr);
1200 * Call external program
1202 * @param navit The navit instance
1203 * @param function unused (needed to match command function signiture)
1204 * @param in input attributes in[0] - name of executable, in[1..] - parameters
1205 * @param out output attribute unused
1206 * @param valid unused
1210 navit_cmd_spawn(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1212 int i,j, nparms, nvalid;
1213 const char ** argv=NULL;
1214 struct spawn_process_info *pi;
1220 if (in[nparms]->type!=attr_none)
1227 argv=g_new(char*,nvalid+1);
1228 for(i=0,j=0;in[i];i++) {
1229 if(in[i]->type!=attr_none ) {
1230 argv[j++]=attr_to_text(in[i],NULL,1);
1232 dbg(0,"Parameter #%i is attr_none - skipping\n",i);
1236 pi=spawn_process(argv);
1238 // spawn_process() testing suite - uncomment following code to test.
1240 // example of non-blocking wait
1241 //int st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1242 // example of blocking wait
1243 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1244 // example of wait after process is finished and status is
1246 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1247 // example of wait after process is finished and status is
1248 // already tested - unblocked
1249 //st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1251 // End testing suite
1252 spawn_process_info_free(pi);
1253 for(i=0;argv[i];i++)
1260 static struct command_table commands[] = {
1261 {"zoom_in",command_cast(navit_cmd_zoom_in)},
1262 {"zoom_out",command_cast(navit_cmd_zoom_out)},
1263 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
1264 {"say",command_cast(navit_cmd_say)},
1265 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
1266 {"set_destination",command_cast(navit_cmd_set_destination)},
1267 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
1268 {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
1269 {"set_int_var",command_cast(navit_cmd_set_int_var)},
1270 {"get_int_var",command_cast(navit_cmd_get_int_var)},
1271 {"push_int",command_cast(navit_cmd_push_int)},
1272 {"pop_int",command_cast(navit_cmd_pop_int)},
1273 {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
1274 {"toggle_layer",command_cast(navit_cmd_toggle_layer)},
1275 {"strjoin",command_cast(navit_cmd_strjoin)},
1276 {"spawn",command_cast(navit_cmd_spawn)},
1277 {"map_add_curr_pos",command_cast(navit_cmd_map_add_curr_pos)},
1278 {"map_item_set_attr",command_cast(navit_cmd_map_item_set_attr)},
1279 {"set_attr_var",command_cast(navit_cmd_set_attr_var)},
1280 {"get_attr_var",command_cast(navit_cmd_get_attr_var)},
1284 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
1286 command_add_table(this_->attr_cbl, commands, count, this_);
1290 navit_new(struct attr *parent, struct attr **attrs)
1292 struct navit *this_=g_new0(struct navit, 1);
1293 struct pcoord center;
1296 enum projection pro=projection_mg;
1301 this_->self.type=attr_navit;
1302 this_->self.u.navit=this_;
1303 this_->attr_cbl=callback_list_new();
1305 this_->orientation=-1;
1306 this_->tracking_flag=1;
1307 this_->recentdest_count=10;
1308 this_->osd_configuration=-1;
1310 this_->center_timeout = 10;
1311 this_->use_mousewheel = 1;
1312 this_->autozoom_secs = 10;
1313 this_->autozoom_min = 7;
1314 this_->autozoom_active = 0;
1315 this_->zoom_min = 1;
1316 this_->zoom_max = 2097152;
1317 this_->follow_cursor = 1;
1321 this_->trans = transform_new();
1322 this_->trans_cursor = transform_new();
1323 transform_from_geo(pro, &g, &co);
1328 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
1330 this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
1334 for (;*attrs; attrs++) {
1335 navit_set_attr_do(this_, *attrs, 1);
1337 this_->displaylist=graphics_displaylist_new();
1338 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
1340 this_->messages = messagelist_new(attrs);
1346 navit_set_gui(struct navit *this_, struct gui *gui)
1351 if (gui_has_main_loop(this_->gui)) {
1352 if (! main_loop_gui) {
1353 main_loop_gui=this_->gui;
1355 dbg(0,"gui with main loop already active, ignoring this instance");
1363 navit_add_message(struct navit *this_, char *message)
1365 message_new(this_->messages, message);
1369 *navit_get_messages(struct navit *this_)
1371 return message_get(this_->messages);
1375 navit_set_graphics(struct navit *this_, struct graphics *gra)
1380 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1381 graphics_add_callback(gra, this_->resize_callback);
1382 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1383 graphics_add_callback(gra, this_->button_callback);
1384 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1385 graphics_add_callback(gra, this_->motion_callback);
1386 this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1387 graphics_add_callback(gra, this_->predraw_callback);
1392 navit_get_graphics(struct navit *this_)
1397 struct vehicleprofile *
1398 navit_get_vehicleprofile(struct navit *this_)
1400 return this_->vehicleprofile;
1404 navit_get_vehicleprofiles(struct navit *this_)
1406 return this_->vehicleprofiles;
1410 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1415 c=transform_center(this_->trans);
1416 transform_to_geo(transform_get_projection(this_->trans), c, &g);
1417 transform_set_projection(this_->trans, pro);
1418 transform_from_geo(pro, &g, c);
1424 * Start the route computing to a given set of coordinates
1426 * @param navit The navit instance
1427 * @param c The coordinate to start routing to
1428 * @param description A label which allows the user to later identify this destination in the former destinations selection
1432 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1434 char *destination_file;
1436 this_->destination=*c;
1437 this_->destination_valid=1;
1439 dbg(1, "navit->navit_set_destination %i\n", c->x);
1440 dbg(1, "navit->navit_set_destination %i\n", c->y);
1443 this_->destination_valid=0;
1444 destination_file = bookmarks_get_destination_file(TRUE);
1445 bookmarks_append_coord(this_->bookmarks, destination_file, c, 1, "former_destination", description, NULL, this_->recentdest_count);
1446 g_free(destination_file);
1447 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1449 route_set_destination(this_->route, c, async);
1451 if (this_->ready == 3)
1457 * Start the route computing to a given set of coordinates including waypoints
1459 * @param navit The navit instance
1460 * @param c The coordinate to start routing to
1461 * @param description A label which allows the user to later identify this destination in the former destinations selection
1465 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1467 char *destination_file;
1469 this_->destination=c[count-1];
1470 this_->destination_valid=1;
1472 this_->destination_valid=0;
1473 destination_file = bookmarks_get_destination_file(TRUE);
1474 bookmarks_append_coord(this_->bookmarks, destination_file, c, count, "former_itinerary", description, NULL, this_->recentdest_count);
1475 g_free(destination_file);
1476 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1478 route_set_destinations(this_->route, c, count, async);
1480 if (this_->ready == 3)
1486 * @brief Checks if a route is calculated
1488 * This function checks if a route is calculated.
1490 * @param this_ The navit struct whose route should be checked.
1491 * @return True if the route is set, false otherwise.
1494 navit_check_route(struct navit *this_)
1497 return route_get_path_set(this_->route);
1504 navit_former_destinations_active(struct navit *this_)
1506 char *destination_file = bookmarks_get_destination_file(FALSE);
1510 f=fopen(destination_file,"r");
1512 if(!fseek(f, -2, SEEK_END) && fread(buffer, 2, 1, f) == 1 && (buffer[0]!='\n' || buffer[1]!='\n'))
1516 g_free(destination_file);
1521 navit_add_former_destinations_from_file(struct navit *this_)
1523 char *destination_file = bookmarks_get_destination_file(FALSE);
1524 struct attr *attrs[4];
1525 struct map_rect *mr;
1527 int i,valid=0,count=0;
1529 struct pcoord pc[16];
1535 parent.type=attr_navit;
1536 parent.u.navit=this_;
1538 type.type=attr_type;
1539 type.u.str="textfile";
1541 data.type=attr_data;
1542 data.u.str=destination_file;
1544 flags.type=attr_flags;
1547 attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1549 this_->former_destination=map_new(&parent, attrs);
1550 g_free(destination_file);
1551 if (!this_->route || !navit_former_destinations_active(this_))
1553 mr=map_rect_new(this_->former_destination, NULL);
1554 while ((item=map_rect_get_item(mr))) {
1555 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
1558 map_rect_destroy(mr);
1559 if (valid && count > 0) {
1560 for (i = 0 ; i < count ; i++) {
1561 pc[i].pro=map_projection(this_->former_destination);
1566 route_set_destination(this_->route, &pc[0], 1);
1568 route_set_destinations(this_->route, pc, count, 1);
1569 this_->destination=pc[count-1];
1570 this_->destination_valid=1;
1576 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1581 if (this_->textfile_debug_log && this_->vehicle) {
1582 str1=g_strdup_vprintf(fmt, ap);
1583 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1584 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1592 navit_textfile_debug_log_at(struct navit *this_, struct pcoord *pc, const char *fmt, ...)
1597 if (this_->textfile_debug_log && this_->vehicle) {
1598 str1=g_strdup_vprintf(fmt, ap);
1599 str2=g_strdup_printf("0x%x 0x%x%s%s\n", pc->x, pc->y, strlen(str1) ? " " : "", str1);
1600 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1608 navit_say(struct navit *this_, char *text)
1611 speech_say(this_->speech, text);
1616 * @brief Toggles the navigation announcer for navit
1617 * @param this_ The navit object
1620 navit_cmd_announcer_toggle(struct navit *this_)
1622 struct attr attr, speechattr;
1624 // search for the speech attribute
1625 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1627 // find out if the corresponding attribute attr_active has been set
1628 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1630 attr.u.num = !attr.u.num;
1632 // otherwise disable it because voice is enabled by default
1633 attr.type = attr_active;
1637 // apply the new state
1638 if(!speech_set_attr(speechattr.u.speech, &attr))
1641 // announce that the speech attribute has changed
1642 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1646 navit_speak(struct navit *this_)
1648 struct navigation *nav=this_->navigation;
1649 struct map *map=NULL;
1650 struct map_rect *mr=NULL;
1654 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1656 dbg(1, "this_.speech->active %i\n", attr.u.num);
1661 map=navigation_get_map(nav);
1663 mr=map_rect_new(map, NULL);
1665 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1666 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1667 speech_say(this_->speech, attr.u.str);
1668 navit_add_message(this_, attr.u.str);
1669 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1671 map_rect_destroy(mr);
1676 navit_window_roadbook_update(struct navit *this_)
1678 struct navigation *nav=this_->navigation;
1679 struct map *map=NULL;
1680 struct map_rect *mr=NULL;
1683 struct param_list param[5];
1687 datawindow_mode(this_->roadbook_window, 1);
1689 map=navigation_get_map(nav);
1691 mr=map_rect_new(map, NULL);
1692 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1694 dbg(0,"while loop\n");
1695 while ((item=map_rect_get_item(mr))) {
1696 dbg(0,"item=%p\n", item);
1698 if (item->type != type_nav_position) {
1699 item_attr_get(item, attr_navigation_long, &attr);
1700 if (attr.u.str == NULL) {
1703 dbg(2, "Command='%s'\n", attr.u.str);
1704 param[0].value=g_strdup(attr.u.str);
1706 param[0].value=_("Position");
1707 param[0].name=_("Command");
1709 item_attr_get(item, attr_length, &attr);
1710 dbg(2, "Length=%d\n", attr.u.num);
1711 param[1].name=_("Length");
1713 if ( attr.u.num >= 2000 )
1715 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1719 param[1].value=g_strdup_printf("%7d %s",attr.u.num, _("m"));
1722 item_attr_get(item, attr_time, &attr);
1723 dbg(2, "Time=%d\n", attr.u.num);
1725 param[2].name=_("Time");
1728 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1732 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1735 item_attr_get(item, attr_destination_length, &attr);
1736 dbg(2, "Destlength=%d\n", attr.u.num);
1737 param[3].name=_("Destination Length");
1738 if ( attr.u.num >= 2000 )
1740 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1744 param[3].value=g_strdup_printf("%d %s",attr.u.num, _("m"));
1747 item_attr_get(item, attr_destination_time, &attr);
1748 dbg(2, "Desttime=%d\n", attr.u.num);
1750 param[4].name=_("Destination Time");
1753 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1757 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1759 datawindow_add(this_->roadbook_window, param, 5);
1761 map_rect_destroy(mr);
1763 datawindow_mode(this_->roadbook_window, 0);
1767 navit_window_roadbook_destroy(struct navit *this_)
1770 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1771 this_->roadbook_window=NULL;
1772 this_->roadbook_callback=NULL;
1775 navit_window_roadbook_new(struct navit *this_)
1777 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1781 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1782 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1783 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1784 navit_window_roadbook_update(this_);
1788 navit_init(struct navit *this_)
1795 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1797 if (!this_->gui && !(this_->flags & 2)) {
1799 navit_destroy(this_);
1802 if (!this_->gra && !(this_->flags & 1)) {
1803 dbg(0,"no graphics\n");
1804 navit_destroy(this_);
1807 dbg(2,"Connecting gui to graphics\n");
1808 if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1809 struct attr attr_type_gui, attr_type_graphics;
1810 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1811 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1812 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1813 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1814 dbg(0," for explanations and solutions\n");
1816 navit_destroy(this_);
1819 if (this_->speech && this_->navigation) {
1821 speech.type=attr_speech;
1822 speech.u.speech=this_->speech;
1823 navigation_set_attr(this_->navigation, &speech);
1825 dbg(2,"Initializing graphics\n");
1826 dbg(2,"Setting Vehicle\n");
1827 navit_set_vehicle(this_, this_->vehicle);
1828 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1829 if (this_->mapsets) {
1830 struct mapset_handle *msh;
1831 ms=this_->mapsets->data;
1832 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1833 msh=mapset_open(ms);
1834 while (msh && (map=mapset_next(msh, 0))) {
1835 //pass new callback instance for each map in the mapset to make map callback list destruction work correctly
1836 struct callback *pcb = callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1837 map_add_callback(map, pcb);
1842 if ((map=route_get_map(this_->route))) {
1844 map_a.type=attr_map;
1846 mapset_add_attr(ms, &map_a);
1848 if ((map=route_get_graph_map(this_->route))) {
1849 struct attr map_a,active;
1850 map_a.type=attr_map;
1852 active.type=attr_active;
1854 mapset_add_attr(ms, &map_a);
1855 map_set_attr(map, &active);
1857 route_set_mapset(this_->route, ms);
1858 route_set_projection(this_->route, transform_get_projection(this_->trans));
1860 if (this_->tracking) {
1861 tracking_set_mapset(this_->tracking, ms);
1863 tracking_set_route(this_->tracking, this_->route);
1865 if (this_->navigation) {
1866 if ((map=navigation_get_map(this_->navigation))) {
1867 struct attr map_a,active;
1868 map_a.type=attr_map;
1870 active.type=attr_active;
1872 mapset_add_attr(ms, &map_a);
1873 map_set_attr(map, &active);
1876 if (this_->tracking) {
1877 if ((map=tracking_get_map(this_->tracking))) {
1878 struct attr map_a,active;
1879 map_a.type=attr_map;
1881 active.type=attr_active;
1883 mapset_add_attr(ms, &map_a);
1884 map_set_attr(map, &active);
1887 navit_add_former_destinations_from_file(this_);
1890 struct attr callback;
1891 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1892 callback.type=attr_callback;
1893 callback.u.callback=this_->route_cb;
1894 route_add_attr(this_->route, &callback);
1896 if (this_->navigation) {
1897 if (this_->speech) {
1898 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1899 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
1902 navigation_set_route(this_->navigation, this_->route);
1904 dbg(2,"Setting Center\n");
1905 center_file = bookmarks_get_center_file(FALSE);
1906 bookmarks_set_center_from_file(this_->bookmarks, center_file);
1907 g_free(center_file);
1909 if (this_->menubar) {
1910 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
1912 navit_add_menu_windows_items(this_, men);
1918 navit_window_roadbook_new(this_);
1919 navit_window_items_new(this_);
1922 messagelist_init(this_->messages);
1924 navit_set_cursors(this_);
1926 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
1927 callback=(this_->ready == 2);
1929 dbg(2,"ready=%d\n",this_->ready);
1930 if (this_->ready == 3)
1931 navit_draw_async(this_, 1);
1933 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
1935 routech_test(this_);
1940 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
1945 c.x=(r->rl.x+r->lu.x)/2;
1946 c.y=(r->rl.y+r->lu.y)/2;
1947 transform_set_center(this_->trans, &c);
1948 dbg(1,"%x,%x-%x,%x\n", r->rl.x,r->rl.y,r->lu.x,r->lu.y);
1949 while (scale < 1<<20) {
1951 transform_set_scale(this_->trans, scale);
1952 transform_setup_source_rect(this_->trans);
1953 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
1954 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
1955 dbg(1,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
1956 if (p1.x < 0 || p2.x < 0 || p1.x > this_->w || p2.x > this_->w ||
1957 p1.y < 0 || p2.y < 0 || p1.y > this_->h || p2.y > this_->h)
1963 if (this_->ready == 3)
1964 navit_draw_async(this_,0);
1968 navit_zoom_to_route(struct navit *this_, int orientation)
1971 struct map_rect *mr=NULL;
1974 struct coord_rect r;
1979 map=route_get_map(this_->route);
1980 dbg(1,"map=%p\n",map);
1982 mr=map_rect_new(map, NULL);
1983 dbg(1,"mr=%p\n",mr);
1985 while ((item=map_rect_get_item(mr))) {
1986 dbg(1,"item=%s\n", item_to_name(item->type));
1987 while (item_coord_get(item, &c, 1)) {
1992 coord_rect_extend(&r, &c);
1996 map_rect_destroy(mr);
2000 if (orientation != -1)
2001 transform_set_yaw(this_->trans, orientation);
2002 navit_zoom_to_rect(this_, &r);
2006 navit_cmd_zoom_to_route(struct navit *this)
2008 navit_zoom_to_route(this, 0);
2013 * Change the current zoom level
2015 * @param navit The navit instance
2016 * @param center The point where to center the map, including its projection
2020 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
2022 struct coord *c=transform_center(this_->trans);
2024 enum projection pro = transform_get_projection(this_->trans);
2025 if (pro != center->pro) {
2028 transform_from_to(&c1, center->pro, &c2, pro);
2035 navit_set_timeout(this_);
2036 if (this_->ready == 3)
2041 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
2045 transform_set_center(this_->trans, c);
2046 transform_get_size(this_->trans, &width, &height);
2049 update_transformation(this_->trans, &po, p, NULL);
2051 navit_set_timeout(this_);
2055 * Links all vehicles to a cursor depending on the current profile.
2057 * @param this_ A navit instance
2058 * @author Ralph Sennhauser (10/2009)
2061 navit_set_cursors(struct navit *this_)
2064 struct navit_vehicle *nv;
2068 v=g_list_first(this_->vehicles); // GList of navit_vehicles
2071 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
2072 if (!strcmp(name.u.str,"none"))
2075 c=layout_get_cursor(this_->layout_current, name.u.str);
2077 c=layout_get_cursor(this_->layout_current, "default");
2078 vehicle_set_cursor(nv->vehicle, c, 0);
2085 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
2088 struct navit_vehicle *nv=this_->vehicle;
2090 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
2091 #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 */
2092 float min_offset = 0.; // Percent offset at min_offset_speed.
2093 float max_offset = 30.; // Percent offset at max_offset_speed.
2094 int min_offset_speed = 2; // Speed in km/h
2095 int max_offset_speed = 50; // Speed ini km/h
2096 // Calculate cursor offset from the center of the screen, upon speed.
2097 if (nv->speed <= min_offset_speed) {
2098 offset = min_offset;
2099 } else if (nv->speed > max_offset_speed) {
2100 offset = max_offset;
2102 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
2106 transform_get_size(this_->trans, &width, &height);
2107 if (this_->orientation == -1 || keep_orientation) {
2109 p->y=(50 + offset)*height/100;
2111 *dir=keep_orientation?this_->orientation:nv->dir;
2114 if (this_->tracking && this_->tracking_flag) {
2115 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
2117 mdir=nv->dir-this_->orientation;
2120 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
2121 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
2123 *dir=this_->orientation;
2129 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
2133 struct navit_vehicle *nv=this_->vehicle;
2134 navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
2135 transform_set_yaw(this_->trans, dir);
2136 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
2138 navit_autozoom(this_, &nv->coord, nv->speed, 0);
2142 navit_set_center_cursor_draw(struct navit *this_)
2144 navit_set_center_cursor(this_,1,0);
2145 if (this_->ready == 3)
2146 navit_draw_async(this_, 1);
2150 navit_cmd_set_center_cursor(struct navit *this_)
2152 navit_set_center_cursor_draw(this_);
2156 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
2160 transform_reverse(this_->trans, p, &c);
2163 pc.pro = transform_get_projection(this_->trans);
2164 navit_set_center(this_, &pc, set_timeout);
2168 switch((*attrs)->type) {
2170 zoom=(*attrs)->u.num;
2173 g=*((*attrs)->u.coord_geo);
2178 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
2180 int dir=0, orient_old=0, attr_updated=0;
2184 struct navit_vehicle *nv;
2187 active.type=attr_active;
2190 switch (attr->type) {
2192 attr_updated=(this_->autozoom_secs != attr->u.num);
2193 this_->autozoom_secs = attr->u.num;
2195 case attr_autozoom_active:
2196 attr_updated=(this_->autozoom_active != attr->u.num);
2197 this_->autozoom_active = attr->u.num;
2200 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
2201 dbg(1,"0x%x,0x%x\n",co.x,co.y);
2202 transform_set_center(this_->trans, &co);
2204 case attr_drag_bitmap:
2205 attr_updated=(this_->drag_bitmap != !!attr->u.num);
2206 this_->drag_bitmap=!!attr->u.num;
2209 attr_updated=(this_->flags != attr->u.num);
2210 this_->flags=attr->u.num;
2212 case attr_flags_graphics:
2213 attr_updated=(this_->graphics_flags != attr->u.num);
2214 this_->graphics_flags=attr->u.num;
2217 if (!this_->vehicle)
2219 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
2220 this_->vehicle->follow_curr = attr->u.num;
2223 if(this_->layout_current!=attr->u.layout) {
2224 this_->layout_current=attr->u.layout;
2225 graphics_font_destroy_all(this_->gra);
2226 navit_set_cursors(this_);
2227 if (this_->ready == 3)
2232 case attr_layout_name:
2236 if (!strcmp(lay->name,attr->u.str)) {
2238 attr.type=attr_layout;
2240 return navit_set_attr_do(this_, &attr, init);
2245 case attr_map_border:
2246 if (this_->border != attr->u.num) {
2247 this_->border=attr->u.num;
2251 case attr_orientation:
2252 orient_old=this_->orientation;
2253 this_->orientation=attr->u.num;
2255 if (this_->orientation != -1) {
2256 dir = this_->orientation;
2258 if (this_->vehicle) {
2259 dir = this_->vehicle->dir;
2262 transform_set_yaw(this_->trans, dir);
2263 if (orient_old != this_->orientation) {
2265 if (this_->ready == 3)
2272 case attr_osd_configuration:
2273 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
2274 attr_updated=(this_->osd_configuration != attr->u.num);
2275 this_->osd_configuration=attr->u.num;
2278 attr_updated=(this_->pitch != attr->u.num);
2279 this_->pitch=attr->u.num;
2280 transform_set_pitch(this_->trans, this_->pitch);
2281 if (!init && attr_updated && this_->ready == 3)
2284 case attr_projection:
2285 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
2286 navit_projection_set(this_, attr->u.projection, !init);
2291 attr_updated=(this_->radius != attr->u.num);
2292 this_->radius=attr->u.num;
2294 case attr_recent_dest:
2295 attr_updated=(this_->recentdest_count != attr->u.num);
2296 this_->recentdest_count=attr->u.num;
2299 if(this_->speech && this_->speech != attr->u.speech) {
2301 this_->speech = attr->u.speech;
2305 attr_updated=(this_->center_timeout != attr->u.num);
2306 this_->center_timeout = attr->u.num;
2309 attr_updated=(this_->tracking_flag != !!attr->u.num);
2310 this_->tracking_flag=!!attr->u.num;
2312 case attr_transformation:
2313 this_->trans=attr->u.transformation;
2315 case attr_use_mousewheel:
2316 attr_updated=(this_->use_mousewheel != !!attr->u.num);
2317 this_->use_mousewheel=!!attr->u.num;
2323 if (nv->vehicle == attr->u.vehicle) {
2324 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2326 vehicle_set_attr(this_->vehicle->vehicle, &active);
2328 vehicle_set_attr(nv->vehicle, &active);
2331 navit_set_vehicle(this_, nv);
2337 zoom=transform_get_scale(this_->trans);
2338 attr_updated=(zoom != attr->u.num);
2339 transform_set_scale(this_->trans, attr->u.num);
2340 if (attr_updated && !init)
2344 attr_updated=(attr->u.num != this_->zoom_min);
2345 this_->zoom_min=attr->u.num;
2348 attr_updated=(attr->u.num != this_->zoom_max);
2349 this_->zoom_max=attr->u.num;
2352 navit_add_message(this_, attr->u.str);
2354 case attr_follow_cursor:
2355 attr_updated=(this_->follow_cursor != !!attr->u.num);
2356 this_->follow_cursor=!!attr->u.num;
2359 attr_updated=(this_->imperial != attr->u.num);
2360 this_->imperial=attr->u.num;
2365 if (attr_updated && !init) {
2366 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2367 if (attr->type == attr_osd_configuration)
2368 graphics_draw_mode(this_->gra, draw_mode_end);
2374 navit_set_attr(struct navit *this_, struct attr *attr)
2376 return navit_set_attr_do(this_, attr, 0);
2380 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2382 struct message *msg;
2388 msg = navit_get_messages(this_);
2396 len += strlen(msg->text) + 1;
2399 attr->u.str = g_malloc(len + 1);
2401 msg = navit_get_messages(this_);
2404 g_stpcpy((attr->u.str + offset), msg->text);
2405 offset += strlen(msg->text);
2406 attr->u.str[offset] = '\n';
2412 attr->u.str[len] = '\0';
2415 attr->u.num=this_->imperial;
2417 case attr_bookmark_map:
2418 attr->u.map=bookmarks_get_map(this_->bookmarks);
2420 case attr_bookmarks:
2421 attr->u.bookmarks=this_->bookmarks;
2423 case attr_callback_list:
2424 attr->u.callback_list=this_->attr_cbl;
2426 case attr_destination:
2427 if (! this_->destination_valid)
2429 attr->u.pcoord=&this_->destination;
2431 case attr_displaylist:
2432 attr->u.displaylist=this_->displaylist;
2433 return (attr->u.displaylist != NULL);
2435 if (!this_->vehicle)
2437 attr->u.num=this_->vehicle->follow_curr;
2439 case attr_former_destination_map:
2440 attr->u.map=this_->former_destination;
2443 attr->u.graphics=this_->gra;
2444 ret=(attr->u.graphics != NULL);
2447 attr->u.gui=this_->gui;
2448 ret=(attr->u.gui != NULL);
2453 iter->u.list=g_list_next(iter->u.list);
2455 iter->u.list=this_->layouts;
2459 attr->u.layout=(struct layout *)iter->u.list->data;
2461 attr->u.layout=this_->layout_current;
2465 if (iter && this_->mapsets) {
2466 if (!iter->u.mapset_handle) {
2467 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2469 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2471 mapset_close(iter->u.mapset_handle);
2479 attr->u.mapset=this_->mapsets->data;
2480 ret=(attr->u.mapset != NULL);
2482 case attr_navigation:
2483 attr->u.navigation=this_->navigation;
2485 case attr_orientation:
2486 attr->u.num=this_->orientation;
2488 case attr_osd_configuration:
2489 attr->u.num=this_->osd_configuration;
2492 attr->u.num=transform_get_pitch(this_->trans);
2494 case attr_projection:
2496 attr->u.num=transform_get_projection(this_->trans);
2502 attr->u.route=this_->route;
2505 attr->u.speech=this_->speech;
2508 attr->u.num=this_->tracking_flag;
2510 case attr_trackingo:
2511 attr->u.tracking=this_->tracking;
2513 case attr_transformation:
2514 attr->u.transformation=this_->trans;
2519 iter->u.list=g_list_next(iter->u.list);
2521 iter->u.list=this_->vehicles;
2525 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2527 if(this_->vehicle) {
2528 attr->u.vehicle=this_->vehicle->vehicle;
2534 case attr_vehicleprofile:
2535 attr->u.vehicleprofile=this_->vehicleprofile;
2538 attr->u.num=transform_get_scale(this_->trans);
2540 case attr_autozoom_active:
2541 attr->u.num=this_->autozoom_active;
2543 case attr_follow_cursor:
2544 attr->u.num=this_->follow_cursor;
2554 navit_add_log(struct navit *this_, struct log *log)
2556 struct attr type_attr;
2557 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2559 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2560 char *header = "type=track_tracked\n";
2561 if (this_->textfile_debug_log)
2563 log_set_header(log, header, strlen(header));
2564 this_->textfile_debug_log=log;
2571 navit_add_layout(struct navit *this_, struct layout *layout)
2574 this_->layouts = g_list_append(this_->layouts, layout);
2575 layout_get_attr(layout, attr_active, &active, NULL);
2576 if(active.u.num || !this_->layout_current) {
2577 this_->layout_current=layout;
2584 navit_add_attr(struct navit *this_, struct attr *attr)
2587 switch (attr->type) {
2589 navit_add_callback(this_, attr->u.callback);
2592 ret=navit_add_log(this_, attr->u.log);
2595 ret=navit_set_gui(this_, attr->u.gui);
2598 ret=navit_set_graphics(this_, attr->u.graphics);
2601 ret=navit_add_layout(this_, attr->u.layout);
2604 this_->route=attr->u.route;
2607 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2609 case attr_navigation:
2610 this_->navigation=attr->u.navigation;
2612 case attr_recent_dest:
2613 this_->recentdest_count = attr->u.num;
2616 this_->speech=attr->u.speech;
2619 this_->tracking=attr->u.tracking;
2622 ret=navit_add_vehicle(this_, attr->u.vehicle);
2624 case attr_vehicleprofile:
2625 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2627 case attr_autozoom_min:
2628 this_->autozoom_min = attr->u.num;
2633 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2638 navit_remove_attr(struct navit *this_, struct attr *attr)
2641 switch (attr->type) {
2643 navit_remove_callback(this_, attr->u.callback);
2652 navit_attr_iter_new(void)
2654 return g_new0(struct attr_iter, 1);
2658 navit_attr_iter_destroy(struct attr_iter *iter)
2664 navit_add_callback(struct navit *this_, struct callback *cb)
2666 callback_list_add(this_->attr_cbl, cb);
2670 navit_remove_callback(struct navit *this_, struct callback *cb)
2672 callback_list_remove(this_->attr_cbl, cb);
2676 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2678 * @param navit The navit instance
2683 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2685 struct point cursor_pnt;
2686 enum projection pro;
2693 pro=transform_get_projection(this_->trans_cursor);
2696 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2698 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2703 pro=transform_get_projection(this_->trans);
2704 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2707 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2709 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2715 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2717 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2718 struct pcoord cursor_pc;
2719 struct point cursor_pnt, *pnt=&cursor_pnt;
2720 struct tracking *tracking=NULL;
2721 struct pcoord pc[16];
2722 enum projection pro=transform_get_projection(this_->trans_cursor);
2724 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2726 char *destination_file;
2729 if (this_->ready != 3) {
2730 profile(0,"return 1\n");
2733 navit_layout_switch(this_);
2734 if (this_->vehicle == nv && this_->tracking_flag)
2735 tracking=this_->tracking;
2737 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2738 attr_object=tracking;
2739 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2741 attr_object=nv->vehicle;
2742 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2744 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2745 if (!attr_valid.u.num != attr_position_valid_invalid)
2747 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2748 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2749 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2750 profile(0,"return 2\n");
2753 nv->dir=*attr_dir.u.numd;
2754 nv->speed=*attr_speed.u.numd;
2755 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2756 if (nv != this_->vehicle) {
2757 navit_vehicle_draw(this_, nv, NULL);
2758 profile(0,"return 3\n");
2761 cursor_pc.x = nv->coord.x;
2762 cursor_pc.y = nv->coord.y;
2763 cursor_pc.pro = pro;
2766 route_set_position_from_tracking(this_->route, tracking, pro);
2768 route_set_position(this_->route, &cursor_pc);
2770 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2771 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2772 if (this_->gui && nv->speed > 2)
2773 navit_disable_suspend();
2775 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2776 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2777 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2778 navit_set_center_cursor_draw(this_);
2780 navit_vehicle_draw(this_, nv, pnt);
2782 if (nv->follow_curr > 1)
2785 nv->follow_curr=nv->follow;
2786 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2788 /* Finally, if we reached our destination, stop navigation. */
2790 switch(route_destination_reached(this_->route)) {
2792 route_remove_waypoint(this_->route);
2793 count=route_get_destinations(this_->route, pc, 16);
2794 destination_file = bookmarks_get_destination_file(TRUE);
2795 bookmarks_append_coord(this_->bookmarks, destination_file, pc, count, "former_itinerary_part", NULL, NULL, this_->recentdest_count);
2798 navit_set_destination(this_, NULL, NULL, 0);
2802 profile(0,"return 5\n");
2806 * Set the position of the vehicle
2808 * @param navit The navit instance
2809 * @param c The coordinate to set as position
2814 navit_set_position(struct navit *this_, struct pcoord *c)
2817 route_set_position(this_->route, c);
2818 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2820 if (this_->ready == 3)
2825 navit_set_vehicleprofile(struct navit *this_, char *name)
2829 l=this_->vehicleprofiles;
2831 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2832 if (!strcmp(attr.u.str, name)) {
2833 this_->vehicleprofile=l->data;
2835 route_set_profile(this_->route, this_->vehicleprofile);
2845 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2849 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2850 if (navit_set_vehicleprofile(this_, attr.u.str))
2853 if (!navit_set_vehicleprofile(this_,"car")) {
2854 /* We do not have a fallback "car" profile
2855 * so lets set any profile */
2857 l=this_->vehicleprofiles;
2859 this_->vehicleprofile=l->data;
2861 route_set_profile(this_->route, this_->vehicleprofile);
2867 * Register a new vehicle
2869 * @param navit The navit instance
2870 * @param v The vehicle instance
2871 * @returns 1 for success
2874 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2876 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2877 struct attr follow, active, animate;
2882 nv->animate_cursor=0;
2883 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2884 nv->follow=follow.u.num;
2885 nv->follow_curr=nv->follow;
2886 this_->vehicles=g_list_append(this_->vehicles, nv);
2887 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2888 navit_set_vehicle(this_, nv);
2889 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2890 nv->animate_cursor=animate.u.num;
2891 nv->callback.type=attr_callback;
2892 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2893 vehicle_add_attr(nv->vehicle, &nv->callback);
2894 vehicle_set_attr(nv->vehicle, &this_->self);
2902 navit_get_gui(struct navit *this_)
2907 struct transformation *
2908 navit_get_trans(struct navit *this_)
2910 return this_->trans;
2914 navit_get_route(struct navit *this_)
2916 return this_->route;
2920 navit_get_navigation(struct navit *this_)
2922 return this_->navigation;
2925 struct displaylist *
2926 navit_get_displaylist(struct navit *this_)
2928 return this_->displaylist;
2932 navit_layout_switch(struct navit *n)
2936 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
2937 double trise,tset,trise_actual;
2939 int year, month, day;
2941 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
2942 return; //No layout - nothing to switch
2946 l=layout_attr.u.layout;
2948 if (l->dayname || l->nightname) {
2949 //Ok, we know that we have profile to switch
2951 //Check that we aren't calculating too fast
2952 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
2953 currTs=iso8601_to_secs(iso8601_attr.u.str);
2954 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
2956 if (currTs-(n->prevTs)<60) {
2957 //We've have to wait a little
2960 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
2962 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
2963 return; //No valid fix yet
2965 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
2966 //No position - no sun
2970 //We calculate sunrise anyway, cause it is needed both for day and for night
2971 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2972 //near the pole sun never rises/sets, so we should never switch profiles
2973 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
2979 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
2982 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
2983 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
2984 //The sun is rising now!
2985 if (strcmp(l->name,l->dayname)) {
2986 navit_set_layout_by_name(n,l->dayname);
2991 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
2992 //near the pole sun never rises/sets, so we should never switch profiles
2993 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
2997 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
2998 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
2999 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
3000 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
3002 if (strcmp(l->name,l->nightname)) {
3003 navit_set_layout_by_name(n,l->nightname);
3013 navit_set_vehicle_by_name(struct navit *n,const char *name)
3016 struct attr_iter *iter;
3017 struct attr vehicle_attr, name_attr;
3019 iter=navit_attr_iter_new();
3021 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
3022 v=vehicle_attr.u.vehicle;
3023 vehicle_get_attr(v,attr_name,&name_attr,NULL);
3024 if (name_attr.type==attr_name) {
3025 if (!strcmp(name,name_attr.u.str)) {
3026 navit_set_attr(n,&vehicle_attr);
3027 navit_attr_iter_destroy(iter);
3032 navit_attr_iter_destroy(iter);
3037 navit_set_layout_by_name(struct navit *n,const char *name)
3040 struct attr_iter iter;
3041 struct attr layout_attr;
3045 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
3046 return 0; //No layouts - nothing to do
3048 if (iter.u.list==NULL) {
3052 iter.u.list=g_list_first(iter.u.list);
3054 while(iter.u.list) {
3055 l=(struct layout*)iter.u.list->data;
3056 if (!strcmp(name,l->name)) {
3057 layout_attr.u.layout=l;
3058 layout_attr.type=attr_layout;
3059 navit_set_attr(n,&layout_attr);
3060 iter.u.list=g_list_first(iter.u.list);
3063 iter.u.list=g_list_next(iter.u.list);
3066 iter.u.list=g_list_first(iter.u.list);
3071 navit_disable_suspend() {
3072 gui_disable_suspend(global_navit->gui);
3073 callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
3077 navit_block(struct navit *this_, int block)
3080 this_->blocked |= 1;
3081 if (graphics_draw_cancel(this_->gra, this_->displaylist))
3082 this_->blocked |= 2;
3085 if ((this_->blocked & 2) || block < 0) {
3095 navit_destroy(struct navit *this_)
3098 callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
3100 /* TODO: destroy objects contained in this_ */
3102 vehicle_destroy(this_->vehicle->vehicle);
3103 if (this_->bookmarks) {
3104 char *center_file = bookmarks_get_center_file(TRUE);
3105 bookmarks_write_center_to_file(this_->bookmarks, center_file);
3106 g_free(center_file);
3107 bookmarks_destroy(this_->bookmarks);
3109 callback_destroy(this_->nav_speech_cb);
3110 callback_destroy(this_->roadbook_callback);
3111 callback_destroy(this_->popup_callback);
3112 callback_destroy(this_->motion_timeout_callback);
3113 callback_destroy(this_->progress_cb);
3115 graphics_remove_callback(this_->gra, this_->resize_callback);
3116 callback_destroy(this_->resize_callback);
3118 graphics_remove_callback(this_->gra, this_->button_callback);
3119 callback_destroy(this_->button_callback);
3121 graphics_remove_callback(this_->gra, this_->motion_callback);
3122 callback_destroy(this_->motion_callback);
3124 graphics_remove_callback(this_->gra, this_->predraw_callback);
3125 callback_destroy(this_->predraw_callback);
3126 route_destroy(this_->route);
3127 ms = navit_get_mapset(this_);
3130 graphics_free(this_->gra);