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 #include "xmlconfig.h"
71 #ifdef HAVE_API_WIN32_BASE
75 #ifdef HAVE_API_WIN32_CE
79 /* define string for bookmark handling */
80 #define TEXTFILE_COMMENT_NAVI_STOPPED "# navigation stopped\n"
83 * @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
87 //! The navit_vehicule
88 struct navit_vehicle {
90 /*! Limit of the follow counter. See navit_add_vehicle */
92 /*! Deprecated : follow counter itself. When it reaches 'update' counts, map is recentered*/
96 struct coord last; /*< Position of the last update of this vehicle */
97 struct vehicle *vehicle;
103 struct object_func *func;
110 struct layout *layout_current;
111 struct graphics *gra;
112 struct action *action;
113 struct transformation *trans, *trans_cursor;
114 struct compass *compass;
116 struct navigation *navigation;
117 struct speech *speech;
118 struct tracking *tracking;
121 struct displaylist *displaylist;
124 int recentdest_count;
125 int osd_configuration;
127 GList *windows_items;
128 struct navit_vehicle *vehicle;
129 struct callback_list *attr_cbl;
130 struct callback *nav_speech_cb, *roadbook_callback, *popup_callback, *route_cb, *progress_cb;
131 struct datawindow *roadbook_window;
132 struct map *former_destination;
133 struct point pressed, last, current;
134 int button_pressed,moved,popped,zoomed;
139 struct event_timeout *button_timeout, *motion_timeout;
140 struct callback *motion_timeout_callback;
142 int ignore_graphics_events;
143 struct log *textfile_debug_log;
144 struct pcoord destination;
145 int destination_valid;
150 struct messagelist *messages;
151 struct callback *resize_callback,*button_callback,*motion_callback,*predraw_callback;
152 struct vehicleprofile *vehicleprofile;
153 GList *vehicleprofiles;
158 int zoom_min, zoom_max;
160 struct bookmarks *bookmarks;
162 /* 1=No graphics ok */
167 struct attr **attr_list;
170 struct gui *main_loop_gui;
176 struct mapset_handle *mapset_handle;
180 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
181 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
182 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
183 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
184 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
185 static void navit_set_cursors(struct navit *this_);
186 static void navit_cmd_zoom_to_route(struct navit *this);
187 static void navit_cmd_set_center_cursor(struct navit *this_);
188 static void navit_cmd_announcer_toggle(struct navit *this_);
189 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
190 struct object_func navit_func;
192 struct navit *global_navit;
195 navit_add_mapset(struct navit *this_, struct mapset *ms)
197 this_->mapsets = g_list_append(this_->mapsets, ms);
201 navit_get_mapset(struct navit *this_)
204 return this_->mapsets->data;
206 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
212 navit_get_tracking(struct navit *this_)
214 return this_->tracking;
218 * @brief Get the user data directory.
219 * @param[in] create - create the directory if it does not exist
221 * @return char * to the data directory string.
223 * returns the directory used to store user data files (center.txt,
224 * destination.txt, bookmark.txt, ...)
228 navit_get_user_data_directory(int create) {
230 dir = getenv("NAVIT_USER_DATADIR");
231 if (create && !file_exists(dir)) {
232 dbg(0,"creating dir %s\n", dir);
233 if (file_mkdir(dir,0)) {
234 dbg(0,"failed creating dir %s\n", dir);
239 } /* end: navit_get_user_data_directory(gboolean create) */
243 navit_draw_async(struct navit *this_, int async)
246 if (this_->blocked) {
250 transform_setup_source_rect(this_->trans);
251 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
255 navit_draw(struct navit *this_)
257 if (this_->ready == 3)
258 navit_draw_async(this_, 0);
262 navit_get_ready(struct navit *this_)
270 navit_draw_displaylist(struct navit *this_)
272 if (this_->ready == 3)
273 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
277 navit_map_progress(struct navit *this_)
281 struct mapset_handle *msh;
284 if (this_->ready != 3)
289 ms=this_->mapsets->data;
291 while (msh && (map=mapset_next(msh, 0))) {
292 if (map_get_attr(map, attr_progress, &attr, NULL)) {
293 char *str=g_strdup_printf("%s ",attr.u.str);
294 graphics_draw_mode(this_->gra, draw_mode_begin);
295 graphics_draw_text_std(this_->gra, 16, str, &p);
298 graphics_draw_mode(this_->gra, draw_mode_end);
305 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
308 if (attr->type != attr_route_status)
311 if (this_->ready != 3)
313 if (updated != route_status_path_done_new)
315 if (this_->vehicle) {
316 if (this_->vehicle->follow_curr == 1)
318 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
319 this_->vehicle->follow_curr=this_->vehicle->follow;
325 navit_handle_resize(struct navit *this_, int w, int h)
327 struct map_selection sel;
328 int callback=(this_->ready == 1);
330 memset(&sel, 0, sizeof(sel));
335 transform_set_screen_selection(this_->trans, &sel);
336 graphics_init(this_->gra);
337 graphics_set_rect(this_->gra, &sel.u.p_rect);
339 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
340 if (this_->ready == 3)
341 navit_draw_async(this_, 1);
345 navit_resize(void *data, int w, int h)
347 struct navit *this=data;
348 if (!this->ignore_graphics_events)
349 navit_handle_resize(this, w, h);
353 navit_get_width(struct navit *this_)
360 navit_get_height(struct navit *this_)
366 navit_popup(void *data)
368 struct navit *this_=data;
369 popup(this_, 1, &this_->pressed);
370 this_->button_timeout=NULL;
376 navit_ignore_button(struct navit *this_)
378 if (this_->ignore_button)
380 this_->ignore_button=1;
385 navit_ignore_graphics_events(struct navit *this_, int ignore)
387 this_->ignore_graphics_events=ignore;
391 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
396 double angleo,anglen;
398 if (!transform_reverse(tr, old, &co))
401 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
402 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
403 yaw=transform_get_yaw(tr)+angleo-anglen;
404 transform_set_yaw(tr, yaw % 360);
406 if (!transform_reverse(tr, new, &cn))
408 cp=transform_get_center(tr);
411 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
412 transform_set_center(tr, &c);
416 navit_set_timeout(struct navit *this_)
419 follow.type=attr_follow;
420 follow.u.num=this_->center_timeout;
421 navit_set_attr(this_, &follow);
425 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
429 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
430 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
431 if (this_->ignore_button) {
432 this_->ignore_button=0;
440 this_->button_pressed=1;
444 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
447 navit_set_center_screen(this_, p, 1);
449 popup(this_, button, p);
450 if (button == 4 && this_->use_mousewheel) {
452 navit_zoom_in(this_, 2, p);
454 if (button == 5 && this_->use_mousewheel) {
456 navit_zoom_out(this_, 2, p);
460 this_->button_pressed=0;
461 if (this_->button_timeout) {
462 event_remove_timeout(this_->button_timeout);
463 this_->button_timeout=NULL;
464 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
465 navit_set_center_screen(this_, p, !this_->zoomed);
468 if (this_->motion_timeout) {
469 event_remove_timeout(this_->motion_timeout);
470 this_->motion_timeout=NULL;
477 update_transformation(this_->trans, &this_->pressed, p, &pr);
479 update_transformation(this_->trans, &this_->pressed, p, NULL);
481 graphics_draw_drag(this_->gra, NULL);
482 transform_copy(this_->trans, this_->trans_cursor);
483 graphics_overlay_disable(this_->gra, 0);
485 navit_set_timeout(this_);
494 navit_button(void *data, int pressed, int button, struct point *p)
496 struct navit *this=data;
497 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
498 if (!this->ignore_graphics_events) {
499 if (! this->popup_callback)
500 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
501 navit_handle_button(this, pressed, button, p, this->popup_callback);
507 navit_motion_timeout(struct navit *this_)
511 if (this_->drag_bitmap) {
513 point.x=(this_->current.x-this_->pressed.x);
514 point.y=(this_->current.y-this_->pressed.y);
515 if (graphics_draw_drag(this_->gra, &point)) {
516 graphics_overlay_disable(this_->gra, 1);
517 graphics_draw_mode(this_->gra, draw_mode_end);
519 this_->motion_timeout=NULL;
523 dx=(this_->current.x-this_->last.x);
524 dy=(this_->current.y-this_->last.y);
526 struct transformation *tr;
528 this_->last=this_->current;
529 graphics_overlay_disable(this_->gra, 1);
530 tr=transform_dup(this_->trans);
534 update_transformation(tr, &this_->pressed, &this_->current, &pr);
536 update_transformation(tr, &this_->pressed, &this_->current, NULL);
539 graphics_displaylist_move(this_->displaylist, dx, dy);
541 graphics_draw_cancel(this_->gra, this_->displaylist);
542 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
543 transform_destroy(tr);
546 this_->motion_timeout=NULL;
551 navit_handle_motion(struct navit *this_, struct point *p)
555 if (this_->button_pressed && !this_->popped) {
556 dx=(p->x-this_->pressed.x);
557 dy=(p->y-this_->pressed.y);
558 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
560 if (this_->button_timeout) {
561 event_remove_timeout(this_->button_timeout);
562 this_->button_timeout=NULL;
565 if (! this_->motion_timeout_callback)
566 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
567 if (! this_->motion_timeout)
568 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
574 navit_motion(void *data, struct point *p)
576 struct navit *this=data;
577 if (!this->ignore_graphics_events)
578 navit_handle_motion(this, p);
582 navit_predraw(struct navit *this_)
585 struct navit_vehicle *nv;
586 transform_copy(this_->trans, this_->trans_cursor);
590 navit_vehicle_draw(this_, nv, NULL);
596 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
598 struct coord c1, c2, *center;
599 if (scale < this_->zoom_min)
600 scale=this_->zoom_min;
601 if (scale > this_->zoom_max)
602 scale=this_->zoom_max;
604 transform_reverse(this_->trans, p, &c1);
605 transform_set_scale(this_->trans, scale);
607 transform_reverse(this_->trans, p, &c2);
608 center = transform_center(this_->trans);
609 center->x += c1.x - c2.x;
610 center->y += c1.y - c2.y;
617 * @brief Automatically adjusts zoom level
619 * This function automatically adjusts the current
620 * zoom level according to the current speed.
622 * @param this_ The navit struct
623 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
624 * @param speed The vehicles speed in meters per second
625 * @param dir The direction into which the vehicle moves
628 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
635 if (! this_->autozoom_active) {
639 distance = speed * this_->autozoom_secs;
641 transform_get_size(this_->trans, &w, &h);
642 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
643 scale = transform_get_scale(this_->trans);
645 /* We make sure that the point we want to see is within a certain range
646 * around the vehicle. The radius of this circle is the size of the
647 * screen. This doesn't necessarily mean the point is visible because of
648 * perspective etc. Quite rough, but should be enough. */
651 new_scale = (double)distance / h * 16;
653 new_scale = (double)distance / w * 16;
656 if (abs(new_scale - scale) < 2) {
660 if (new_scale >= this_->autozoom_min) {
661 navit_scale(this_, (long)new_scale, &pc, 0);
663 if (scale != this_->autozoom_min) {
664 navit_scale(this_, this_->autozoom_min, &pc, 0);
670 * Change the current zoom level, zooming closer to the ground
672 * @param navit The navit instance
673 * @param factor The zoom factor, usually 2
674 * @param p The invariant point (if set to NULL, default to center)
678 navit_zoom_in(struct navit *this_, int factor, struct point *p)
680 long scale=transform_get_scale(this_->trans)/factor;
683 navit_scale(this_, scale, p, 1);
687 * Change the current zoom level
689 * @param navit The navit instance
690 * @param factor The zoom factor, usually 2
691 * @param p The invariant point (if set to NULL, default to center)
695 navit_zoom_out(struct navit *this_, int factor, struct point *p)
697 long scale=transform_get_scale(this_->trans)*factor;
698 navit_scale(this_, scale, p, 1);
702 navit_zoom_in_cursor(struct navit *this_, int factor)
705 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
706 navit_zoom_in(this_, factor, &p);
707 this_->vehicle->follow_curr=this_->vehicle->follow;
709 navit_zoom_in(this_, factor, NULL);
713 navit_zoom_out_cursor(struct navit *this_, int factor)
716 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
717 navit_zoom_out(this_, 2, &p);
718 this_->vehicle->follow_curr=this_->vehicle->follow;
720 navit_zoom_out(this_, 2, NULL);
724 navit_cmd_zoom_in(struct navit *this_)
726 navit_zoom_in_cursor(this_, 2);
731 navit_cmd_zoom_out(struct navit *this_)
733 navit_zoom_out_cursor(this_, 2);
739 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
741 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
742 navit_say(this, in[0]->u.str);
745 static GHashTable *cmd_int_var_hash = NULL;
746 static GHashTable *cmd_attr_var_hash = NULL;
749 * Store key value pair for the command system (for int typed values)
751 * @param navit The navit instance
752 * @param function unused (needed to match command function signiture)
753 * @param in input attributes in[0] is the key string, in[1] is the integer value to store
754 * @param out output attributes, unused
755 * @param valid unused
759 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
763 if(!cmd_int_var_hash) {
764 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
767 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
768 (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
769 val = g_new(struct attr,1);
770 attr_dup_content(in[1],val);
771 key = g_strdup(in[0]->u.str);
772 g_hash_table_insert(cmd_int_var_hash, key, val);
778 * Store key value pair for the command system (for attr typed values, can be used as opaque handles)
780 * @param navit The navit instance
781 * @param function unused (needed to match command function signiture)
782 * @param in input attributes in[0] is the key string, in[1] is the attr* value to store
783 * @param out output attributes, unused
784 * @param valid unused
787 //TODO free stored attributes on navit_destroy
789 navit_cmd_set_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
793 if(!cmd_attr_var_hash) {
794 cmd_attr_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
797 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
799 val = attr_dup(in[1]);
801 key = g_strdup(in[0]->u.str);
802 g_hash_table_insert(cmd_attr_var_hash, key, val);
804 dbg(1, "Wrong parameters for set_attr_var() command function\n");
811 * command to toggle the active state of a named layer of the current layout
813 * @param navit The navit instance
814 * @param function unused (needed to match command function signiture)
815 * @param in input attribute in[0] is the name of the layer
816 * @param out output unused
817 * @param valid unused
821 navit_cmd_toggle_layer(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
823 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
824 if(this->layout_current && this->layout_current->layers) {
825 GList* layers = this->layout_current->layers;
827 struct layer*l=layers->data;
828 if(l && !strcmp(l->name,in[0]->u.str) ) {
833 layers=g_list_next(layers);
840 * adds an item with the current coordinate of the vehicle to a named map
842 * @param navit The navit instance
843 * @param function unused (needed to match command function signiture)
844 * @param in input attribute in[0] is the name of the map
845 * @param out output attribute, 0 on error or the id of the created item on success
846 * @param valid unused
850 navit_cmd_map_add_curr_pos(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
852 struct attr **list = g_new0(struct attr *,2);
853 struct attr*val = g_new0(struct attr,1);
855 struct map_selection sel;
856 const int selection_range = 10;
857 enum item_type item_type;
859 struct map* curr_map = NULL;
860 struct coord curr_coord;
863 //return invalid item on error
864 val->type = attr_none;
870 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str && //map name
871 in[1] && ATTR_IS_STRING(in[1]->type) && in[1]->u.str //item type
874 if(!(ms=navit_get_mapset(this))) {
875 dbg(1, "Command function map_add_curr_pos(): there is no active mapset\n");
879 if((item_type = item_from_name(in[1]->u.str))==type_none) {
880 dbg(1, "Command function map_add_curr_pos(): unknown item type\n");
884 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
886 //no map with the given name found
888 dbg(1, "Command function map_add_curr_pos(): map not found\n");
892 if(this->vehicle && this->vehicle->vehicle ) {
893 struct attr pos_attr;
894 if(vehicle_get_attr(this->vehicle->vehicle,attr_position_coord_geo,&pos_attr,NULL)) {
895 transform_from_geo(projection_mg, pos_attr.u.coord_geo, &curr_coord);
897 dbg(1, "Command function map_add_curr_pos(): vehicle position is not accessible\n");
901 dbg(1, "Command function map_add_curr_pos(): no vehicle\n");
907 sel.range.min=type_none;
908 sel.range.max=type_tec_common;
909 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
910 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
911 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
912 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
914 mr = map_rect_new(curr_map, &sel);
916 it = map_rect_create_item( mr, item_type);
918 val->type = attr_type_item_begin;
919 item_coord_set(it,&curr_coord, 1, change_mode_modify);
923 map_rect_destroy(mr);
928 * sets an attribute (name value pair) of a map item specified by map name and item id
930 * @param navit The navit instance
931 * @param function unused (needed to match command function signiture)
932 * @param in input attribute in[0] - name of the map ; in[1] - item ; in[2] - attr name ; in[3] - attr value
933 * @param out output attribute, 0 on error, 1 on success
934 * @param valid unused
938 navit_cmd_map_item_set_attr(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
941 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str &&//map name
942 in[1] && ATTR_IS_ITEM(in[1]->type) && //item
943 in[2] && ATTR_IS_STRING(in[2]->type) && in[2]->u.str && //attr_type str
944 in[3] && ATTR_IS_STRING(in[3]->type) && in[3]->u.str //attr_value str
946 struct attr attr_to_set;
947 struct map* curr_map = NULL;
949 struct map_selection sel;
950 const int selection_range = 500;
951 struct coord curr_coord;
954 if(ATTR_IS_STRING(attr_from_name(in[2]->u.str))) {
955 attr_to_set.u.str = in[3]->u.str;
956 attr_to_set.type = attr_from_name(in[2]->u.str);
958 else if(ATTR_IS_INT(attr_from_name(in[2]->u.str))) {
959 attr_to_set.u.num = atoi(in[3]->u.str);
960 attr_to_set.type = attr_from_name(in[2]->u.str);
962 else if(ATTR_IS_DOUBLE(attr_from_name(in[2]->u.str))) {
963 double* val = g_new0(double,1);
964 *val = atof(in[3]->u.str);
965 attr_to_set.u.numd = val;
966 attr_to_set.type = attr_from_name(in[2]->u.str);
969 ms = navit_get_mapset(this);
971 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
978 sel.range.min=type_none;
979 sel.range.max=type_tec_common;
980 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
981 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
982 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
983 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
987 item_attr_set(it, &attr_to_set, change_mode_modify);
990 dbg(4,"Error in command function item_set_attr()\n");
991 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);
992 dbg(4,"Command function item_set_attr(): item cond: %d\n",(in[1] && ATTR_IS_ITEM(in[1]->type))?1:0);
993 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);
994 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);
999 * Get attr variable given a key string for the command system (for opaque usage)
1001 * @param navit The navit instance
1002 * @param function unused (needed to match command function signiture)
1003 * @param in input attribute in[0] is the key string
1004 * @param out output attribute, the attr for the given key string if exists or NULL
1005 * @param valid unused
1009 navit_cmd_get_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1011 struct attr **list = g_new0(struct attr *,2);
1012 if(!cmd_attr_var_hash) {
1013 struct attr*val = g_new0(struct attr,1);
1014 cmd_attr_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
1015 val->type = attr_type_item_begin;
1019 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
1020 struct attr*ret = g_hash_table_lookup(cmd_attr_var_hash, in[0]->u.str);
1022 list[0] = attr_dup(ret);
1025 struct attr*val = g_new0(struct attr,1);
1026 val->type = attr_type_int_begin;
1037 * Get value given a key string for the command system
1039 * @param navit The navit instance
1040 * @param function unused (needed to match command function signiture)
1041 * @param in input attribute in[0] is the key string
1042 * @param out output attribute, the value for the given key string if exists or 0
1043 * @param valid unused
1047 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1049 struct attr **list = g_new0(struct attr *,2);
1050 if(!cmd_int_var_hash) {
1051 struct attr*val = g_new0(struct attr,1);
1052 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
1053 val->type = attr_type_int_begin;
1057 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
1058 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
1060 list[0] = attr_dup(ret);
1063 struct attr*val = g_new0(struct attr,1);
1064 val->type = attr_type_int_begin;
1073 GList *cmd_int_var_stack = NULL;
1076 * Push an integer to the stack for the command system
1078 * @param navit The navit instance
1079 * @param function unused (needed to match command function signiture)
1080 * @param in input attribute in[0] is the integer attibute to push
1081 * @param out output attributes, unused
1082 * @param valid unused
1086 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1088 if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
1089 struct attr*val = g_new(struct attr,1);
1090 attr_dup_content(in[0],val);
1091 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
1096 * Pop an integer from the command system's integer stack
1098 * @param navit The navit instance
1099 * @param function unused (needed to match command function signiture)
1100 * @param in input attributes unused
1101 * @param out output attribute, the value popped if stack isn't empty or 0
1102 * @param valid unused
1106 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1108 struct attr **list = g_new0(struct attr *,2);
1109 if(!cmd_int_var_stack) {
1110 struct attr*val = g_new0(struct attr,1);
1111 val->type = attr_type_int_begin;
1116 list[0] = cmd_int_var_stack->data;
1117 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1124 * Get current size of command system's integer stack
1126 * @param navit The navit instance
1127 * @param function unused (needed to match command function signiture)
1128 * @param in input attributes unused
1129 * @param out output attribute, the size of stack
1130 * @param valid unused
1134 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1137 struct attr *attr = g_new0(struct attr ,1);
1138 attr->type = attr_type_int_begin;
1139 if(!cmd_int_var_stack) {
1143 attr->u.num = g_list_length(cmd_int_var_stack);
1145 list = g_new0(struct attr *,2);
1149 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1152 static struct attr **
1153 navit_get_coord(struct navit *this, struct attr **in, struct pcoord *pc)
1159 pc->pro = transform_get_projection(this->trans);
1160 if (ATTR_IS_STRING(in[0]->type)) {
1162 coord_parse(in[0]->u.str, pc->pro, &c);
1166 } else if (ATTR_IS_COORD(in[0]->type)) {
1167 pc->x=in[0]->u.coord->x;
1168 pc->y=in[0]->u.coord->y;
1170 } else if (ATTR_IS_PCOORD(in[0]->type)) {
1171 *pc=*in[0]->u.pcoord;
1173 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
1174 pc->pro=in[0]->u.num;
1178 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
1188 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1191 char *description=NULL;
1192 in=navit_get_coord(this, in, &pc);
1195 if (in[0] && ATTR_IS_STRING(in[0]->type))
1196 description=in[0]->u.str;
1197 navit_set_destination(this, &pc, description, 1);
1202 navit_cmd_route_remove_next_waypoint(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1204 navit_remove_waypoint(this);
1209 navit_cmd_route_remove_last_waypoint(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1211 navit_remove_nth_waypoint(this, navit_get_destination_count(this)-1);
1216 navit_cmd_set_center(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1219 in=navit_get_coord(this, in, &pc);
1222 navit_set_center(this, &pc, 0);
1227 navit_cmd_set_position(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1230 in=navit_get_coord(this, in, &pc);
1233 navit_set_position(this, &pc);
1238 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1241 attr.type=attr_type_string_begin;
1242 attr.u.str="Fix me";
1244 *out=attr_generic_add_attr(*out, &attr);
1249 * Join several string attributes into one
1251 * @param navit The navit instance
1252 * @param function unused (needed to match command function signiture)
1253 * @param in input attributes in[0] - separator, in[1..] - attributes to join
1254 * @param out output attribute joined attribute as string
1255 * @param valid unused
1259 navit_cmd_strjoin(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1264 attr.type=attr_type_string_begin;
1266 if(in[0] && in[1]) {
1267 sep=attr_to_text(in[0],NULL,1);
1268 ret=attr_to_text(in[1],NULL,1);
1269 for(i=2;in[i];i++) {
1270 gchar *in_i=attr_to_text(in[i],NULL,1);
1271 gchar *r=g_strjoin(sep,ret,in_i,NULL);
1279 *out=attr_generic_add_attr(*out, &attr);
1286 * Call external program
1288 * @param navit The navit instance
1289 * @param function unused (needed to match command function signiture)
1290 * @param in input attributes in[0] - name of executable, in[1..] - parameters
1291 * @param out output attribute unused
1292 * @param valid unused
1296 navit_cmd_spawn(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1298 int i,j, nparms, nvalid;
1299 const char ** argv=NULL;
1300 struct spawn_process_info *pi;
1306 if (in[nparms]->type!=attr_none)
1313 argv=g_new(char*,nvalid+1);
1314 for(i=0,j=0;in[i];i++) {
1315 if(in[i]->type!=attr_none ) {
1316 argv[j++]=attr_to_text(in[i],NULL,1);
1318 dbg(0,"Parameter #%i is attr_none - skipping\n",i);
1322 pi=spawn_process(argv);
1324 // spawn_process() testing suite - uncomment following code to test.
1326 // example of non-blocking wait
1327 //int st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1328 // example of blocking wait
1329 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1330 // example of wait after process is finished and status is
1332 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1333 // example of wait after process is finished and status is
1334 // already tested - unblocked
1335 //st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1337 // End testing suite
1338 spawn_process_info_free(pi);
1339 for(i=0;argv[i];i++)
1346 static struct command_table commands[] = {
1347 {"zoom_in",command_cast(navit_cmd_zoom_in)},
1348 {"zoom_out",command_cast(navit_cmd_zoom_out)},
1349 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
1350 {"say",command_cast(navit_cmd_say)},
1351 {"set_center",command_cast(navit_cmd_set_center)},
1352 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
1353 {"set_destination",command_cast(navit_cmd_set_destination)},
1354 {"route_remove_next_waypoint",command_cast(navit_cmd_route_remove_next_waypoint)},
1355 {"route_remove_last_waypoint",command_cast(navit_cmd_route_remove_last_waypoint)},
1356 {"set_position",command_cast(navit_cmd_set_position)},
1357 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
1358 {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
1359 {"set_int_var",command_cast(navit_cmd_set_int_var)},
1360 {"get_int_var",command_cast(navit_cmd_get_int_var)},
1361 {"push_int",command_cast(navit_cmd_push_int)},
1362 {"pop_int",command_cast(navit_cmd_pop_int)},
1363 {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
1364 {"toggle_layer",command_cast(navit_cmd_toggle_layer)},
1365 {"strjoin",command_cast(navit_cmd_strjoin)},
1366 {"spawn",command_cast(navit_cmd_spawn)},
1367 {"map_add_curr_pos",command_cast(navit_cmd_map_add_curr_pos)},
1368 {"map_item_set_attr",command_cast(navit_cmd_map_item_set_attr)},
1369 {"set_attr_var",command_cast(navit_cmd_set_attr_var)},
1370 {"get_attr_var",command_cast(navit_cmd_get_attr_var)},
1374 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
1376 command_add_table(this_->attr_cbl, commands, count, this_);
1380 navit_new(struct attr *parent, struct attr **attrs)
1382 struct navit *this_=g_new0(struct navit, 1);
1383 struct pcoord center;
1386 enum projection pro=projection_mg;
1391 this_->func=&navit_func;
1393 this_->attrs=attr_list_dup(attrs);
1394 this_->self.type=attr_navit;
1395 this_->self.u.navit=this_;
1396 this_->attr_cbl=callback_list_new();
1398 this_->orientation=-1;
1399 this_->tracking_flag=1;
1400 this_->recentdest_count=10;
1401 this_->osd_configuration=-1;
1403 this_->center_timeout = 10;
1404 this_->use_mousewheel = 1;
1405 this_->autozoom_secs = 10;
1406 this_->autozoom_min = 7;
1407 this_->autozoom_active = 0;
1408 this_->zoom_min = 1;
1409 this_->zoom_max = 2097152;
1410 this_->follow_cursor = 1;
1414 this_->trans = transform_new();
1415 this_->trans_cursor = transform_new();
1416 transform_from_geo(pro, &g, &co);
1421 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
1423 this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
1427 for (;*attrs; attrs++) {
1428 navit_set_attr_do(this_, *attrs, 1);
1430 this_->displaylist=graphics_displaylist_new();
1431 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
1433 this_->messages = messagelist_new(attrs);
1435 dbg(0,"return %p\n",this_);
1441 navit_set_gui(struct navit *this_, struct gui *gui)
1446 if (gui_has_main_loop(this_->gui)) {
1447 if (! main_loop_gui) {
1448 main_loop_gui=this_->gui;
1450 dbg(0,"gui with main loop already active, ignoring this instance");
1458 navit_add_message(struct navit *this_, char *message)
1460 message_new(this_->messages, message);
1464 *navit_get_messages(struct navit *this_)
1466 return message_get(this_->messages);
1470 navit_set_graphics(struct navit *this_, struct graphics *gra)
1475 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1476 graphics_add_callback(gra, this_->resize_callback);
1477 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1478 graphics_add_callback(gra, this_->button_callback);
1479 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1480 graphics_add_callback(gra, this_->motion_callback);
1481 this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1482 graphics_add_callback(gra, this_->predraw_callback);
1487 navit_get_graphics(struct navit *this_)
1492 struct vehicleprofile *
1493 navit_get_vehicleprofile(struct navit *this_)
1495 return this_->vehicleprofile;
1499 navit_get_vehicleprofiles(struct navit *this_)
1501 return this_->vehicleprofiles;
1505 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1510 c=transform_center(this_->trans);
1511 transform_to_geo(transform_get_projection(this_->trans), c, &g);
1512 transform_set_projection(this_->trans, pro);
1513 transform_from_geo(pro, &g, c);
1519 navit_mark_navigation_stopped(char *former_destination_file){
1521 f=fopen(former_destination_file, "a");
1523 fprintf(f,"%s", TEXTFILE_COMMENT_NAVI_STOPPED);
1526 dbg(0, "Error setting mark in destination file %s: %s\n", former_destination_file, strerror(errno));
1531 * Start or add a given set of coordinates for route computing
1533 * @param navit The navit instance
1534 * @param c The coordinate to start routing to
1535 * @param description A label which allows the user to later identify this destination in the former destinations selection
1539 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1541 char *destination_file;
1542 destination_file = bookmarks_get_destination_file(TRUE);
1544 this_->destination=*c;
1545 this_->destination_valid=1;
1547 dbg(1, "c=(%i,%i)\n", c->x,c->y);
1548 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_destination, description, this_->recentdest_count);
1550 this_->destination_valid=0;
1551 navit_mark_navigation_stopped(destination_file);
1553 g_free(destination_file);
1554 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1557 navit_get_attr(this_, attr_waypoints_flag, &attr, NULL);
1558 if (this_->waypoints_flag==0 || route_get_destination_count(this_->route)==0){
1559 route_set_destination(this_->route, c, async);
1561 route_append_destination(this_->route, c, async);
1564 if (this_->ready == 3)
1570 * Start the route computing to a given set of coordinates including waypoints
1572 * @param navit The navit instance
1573 * @param c The coordinate to start routing to
1574 * @param description A label which allows the user to later identify this destination in the former destinations selection
1578 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1580 char *destination_file;
1582 this_->destination=c[count-1];
1583 this_->destination_valid=1;
1585 destination_file = bookmarks_get_destination_file(TRUE);
1586 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_itinerary, description, this_->recentdest_count);
1587 g_free(destination_file);
1589 this_->destination_valid=0;
1590 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1592 route_set_destinations(this_->route, c, count, async);
1594 if (this_->ready == 3)
1600 navit_get_destinations(struct navit *this_, struct pcoord *pc, int count)
1604 return route_get_destinations(this_->route, pc, count);
1609 navit_get_destination_count(struct navit *this_)
1613 return route_get_destination_count(this_->route);
1617 navit_get_destination_description(struct navit *this_, int n)
1621 return route_get_destination_description(this_->route, n);
1625 navit_remove_nth_waypoint(struct navit *this_, int n)
1629 if (route_get_destination_count(this_->route)>1){
1630 route_remove_nth_waypoint(this_->route, n);
1632 navit_set_destination(this_, NULL, NULL, 0);
1637 navit_remove_waypoint(struct navit *this_)
1641 if (route_get_destination_count(this_->route)>1){
1642 route_remove_waypoint(this_->route);
1644 navit_set_destination(this_, NULL, NULL, 0);
1649 * @brief Checks if a route is calculated
1651 * This function checks if a route is calculated.
1653 * @param this_ The navit struct whose route should be checked.
1654 * @return True if the route is set, false otherwise.
1657 navit_check_route(struct navit *this_)
1660 return route_get_path_set(this_->route);
1667 navit_former_destinations_active(struct navit *this_)
1669 char *destination_file_name = bookmarks_get_destination_file(FALSE);
1670 FILE *destination_file;
1673 destination_file=fopen(destination_file_name,"r");
1674 if (destination_file) {
1675 while(fgets(lastline, sizeof(lastline), destination_file));
1676 fclose(destination_file);
1677 if ((lastline != NULL) && (strcmp(lastline, TEXTFILE_COMMENT_NAVI_STOPPED))){
1681 g_free(destination_file_name);
1685 struct map* read_former_destinations_from_file(){
1686 struct attr type, data, flags, *attrs[4];
1687 char *destination_file = bookmarks_get_destination_file(FALSE);
1690 type.type=attr_type;
1691 type.u.str="textfile";
1693 data.type=attr_data;
1694 data.u.str=destination_file;
1696 flags.type=attr_flags;
1699 attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1701 m=map_new(NULL, attrs);
1702 g_free(destination_file);
1707 navit_add_former_destinations_from_file(struct navit *this_)
1710 int i,valid=0,count=0;
1712 struct pcoord pc[16];
1713 struct map_rect *mr;
1715 this_->former_destination=read_former_destinations_from_file();
1716 if (!this_->route || !navit_former_destinations_active(this_))
1718 mr=map_rect_new(this_->former_destination, NULL);
1719 while ((item=map_rect_get_item(mr))) {
1720 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
1723 map_rect_destroy(mr);
1724 if (valid && count > 0) {
1725 for (i = 0 ; i < count ; i++) {
1726 pc[i].pro=map_projection(this_->former_destination);
1731 route_set_destination(this_->route, &pc[0], 1);
1733 route_set_destinations(this_->route, pc, count, 1);
1734 this_->destination=pc[count-1];
1735 this_->destination_valid=1;
1741 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1746 if (this_->textfile_debug_log && this_->vehicle) {
1747 str1=g_strdup_vprintf(fmt, ap);
1748 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1749 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1757 navit_textfile_debug_log_at(struct navit *this_, struct pcoord *pc, const char *fmt, ...)
1762 if (this_->textfile_debug_log && this_->vehicle) {
1763 str1=g_strdup_vprintf(fmt, ap);
1764 str2=g_strdup_printf("0x%x 0x%x%s%s\n", pc->x, pc->y, strlen(str1) ? " " : "", str1);
1765 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1773 navit_say(struct navit *this_, char *text)
1777 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1779 dbg(1, "this_.speech->active %i\n", attr.u.num);
1781 speech_say(this_->speech, text);
1786 * @brief Toggles the navigation announcer for navit
1787 * @param this_ The navit object
1790 navit_cmd_announcer_toggle(struct navit *this_)
1792 struct attr attr, speechattr;
1794 // search for the speech attribute
1795 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1797 // find out if the corresponding attribute attr_active has been set
1798 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1800 attr.u.num = !attr.u.num;
1802 // otherwise disable it because voice is enabled by default
1803 attr.type = attr_active;
1807 // apply the new state
1808 if(!speech_set_attr(speechattr.u.speech, &attr))
1811 // announce that the speech attribute has changed
1812 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1816 navit_speak(struct navit *this_)
1818 struct navigation *nav=this_->navigation;
1819 struct map *map=NULL;
1820 struct map_rect *mr=NULL;
1824 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1826 dbg(1, "this_.speech->active %i\n", attr.u.num);
1831 map=navigation_get_map(nav);
1833 mr=map_rect_new(map, NULL);
1835 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1836 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1837 speech_say(this_->speech, attr.u.str);
1838 navit_add_message(this_, attr.u.str);
1839 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1841 map_rect_destroy(mr);
1846 navit_window_roadbook_update(struct navit *this_)
1848 struct navigation *nav=this_->navigation;
1849 struct map *map=NULL;
1850 struct map_rect *mr=NULL;
1853 struct param_list param[5];
1857 datawindow_mode(this_->roadbook_window, 1);
1859 map=navigation_get_map(nav);
1861 mr=map_rect_new(map, NULL);
1862 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1864 dbg(0,"while loop\n");
1865 while ((item=map_rect_get_item(mr))) {
1866 dbg(0,"item=%p\n", item);
1868 if (item->type != type_nav_position) {
1869 item_attr_get(item, attr_navigation_long, &attr);
1870 if (attr.u.str == NULL) {
1873 dbg(2, "Command='%s'\n", attr.u.str);
1874 param[0].value=g_strdup(attr.u.str);
1876 param[0].value=_("Position");
1877 param[0].name=_("Command");
1879 item_attr_get(item, attr_length, &attr);
1880 dbg(2, "Length=%d\n", attr.u.num);
1881 param[1].name=_("Length");
1883 if ( attr.u.num >= 2000 )
1885 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1889 param[1].value=g_strdup_printf("%7ld %s",attr.u.num, _("m"));
1892 item_attr_get(item, attr_time, &attr);
1893 dbg(2, "Time=%d\n", attr.u.num);
1895 param[2].name=_("Time");
1898 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1902 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1905 item_attr_get(item, attr_destination_length, &attr);
1906 dbg(2, "Destlength=%d\n", attr.u.num);
1907 param[3].name=_("Destination Length");
1908 if ( attr.u.num >= 2000 )
1910 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1914 param[3].value=g_strdup_printf("%ld %s",attr.u.num, _("m"));
1917 item_attr_get(item, attr_destination_time, &attr);
1918 dbg(2, "Desttime=%d\n", attr.u.num);
1920 param[4].name=_("Destination Time");
1923 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1927 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1929 datawindow_add(this_->roadbook_window, param, 5);
1931 map_rect_destroy(mr);
1933 datawindow_mode(this_->roadbook_window, 0);
1937 navit_window_roadbook_destroy(struct navit *this_)
1940 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1941 callback_destroy(this_->roadbook_callback);
1942 this_->roadbook_window=NULL;
1943 this_->roadbook_callback=NULL;
1946 navit_window_roadbook_new(struct navit *this_)
1948 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1952 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1953 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1954 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1955 navit_window_roadbook_update(this_);
1959 navit_init(struct navit *this_)
1966 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1968 if (!this_->gui && !(this_->flags & 2)) {
1970 navit_destroy(this_);
1973 if (!this_->gra && !(this_->flags & 1)) {
1974 dbg(0,"no graphics\n");
1975 navit_destroy(this_);
1978 dbg(2,"Connecting gui to graphics\n");
1979 if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1980 struct attr attr_type_gui, attr_type_graphics;
1981 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1982 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1983 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1984 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1985 dbg(0," for explanations and solutions\n");
1987 navit_destroy(this_);
1990 if (this_->speech && this_->navigation) {
1992 speech.type=attr_speech;
1993 speech.u.speech=this_->speech;
1994 navigation_set_attr(this_->navigation, &speech);
1996 dbg(2,"Initializing graphics\n");
1997 dbg(2,"Setting Vehicle\n");
1998 navit_set_vehicle(this_, this_->vehicle);
1999 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
2000 if (this_->mapsets) {
2001 struct mapset_handle *msh;
2002 ms=this_->mapsets->data;
2003 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
2004 msh=mapset_open(ms);
2005 while (msh && (map=mapset_next(msh, 0))) {
2006 //pass new callback instance for each map in the mapset to make map callback list destruction work correctly
2007 struct callback *pcb = callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
2008 map_add_callback(map, pcb);
2013 if ((map=route_get_map(this_->route))) {
2015 map_a.type=attr_map;
2017 mapset_add_attr(ms, &map_a);
2019 if ((map=route_get_graph_map(this_->route))) {
2020 struct attr map_a,active;
2021 map_a.type=attr_map;
2023 active.type=attr_active;
2025 mapset_add_attr(ms, &map_a);
2026 map_set_attr(map, &active);
2028 route_set_mapset(this_->route, ms);
2029 route_set_projection(this_->route, transform_get_projection(this_->trans));
2031 if (this_->tracking) {
2032 tracking_set_mapset(this_->tracking, ms);
2034 tracking_set_route(this_->tracking, this_->route);
2036 if (this_->navigation) {
2037 if ((map=navigation_get_map(this_->navigation))) {
2038 struct attr map_a,active;
2039 map_a.type=attr_map;
2041 active.type=attr_active;
2043 mapset_add_attr(ms, &map_a);
2044 map_set_attr(map, &active);
2047 if (this_->tracking) {
2048 if ((map=tracking_get_map(this_->tracking))) {
2049 struct attr map_a,active;
2050 map_a.type=attr_map;
2052 active.type=attr_active;
2054 mapset_add_attr(ms, &map_a);
2055 map_set_attr(map, &active);
2058 navit_add_former_destinations_from_file(this_);
2061 struct attr callback;
2062 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
2063 callback.type=attr_callback;
2064 callback.u.callback=this_->route_cb;
2065 route_add_attr(this_->route, &callback);
2067 if (this_->navigation) {
2068 if (this_->speech) {
2069 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
2070 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
2073 navigation_set_route(this_->navigation, this_->route);
2075 dbg(2,"Setting Center\n");
2076 center_file = bookmarks_get_center_file(FALSE);
2077 bookmarks_set_center_from_file(this_->bookmarks, center_file);
2078 g_free(center_file);
2080 if (this_->menubar) {
2081 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
2083 navit_add_menu_windows_items(this_, men);
2089 navit_window_roadbook_new(this_);
2090 navit_window_items_new(this_);
2093 messagelist_init(this_->messages);
2095 navit_set_cursors(this_);
2097 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
2098 callback=(this_->ready == 2);
2100 dbg(2,"ready=%d\n",this_->ready);
2101 if (this_->ready == 3)
2102 navit_draw_async(this_, 1);
2104 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
2106 routech_test(this_);
2111 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
2116 c.x=(r->rl.x+r->lu.x)/2;
2117 c.y=(r->rl.y+r->lu.y)/2;
2118 transform_set_center(this_->trans, &c);
2119 transform_get_size(this_->trans, &w, &h);
2120 dbg(0,"center 0x%x,0x%x w %d h %d\n",c.x,c.y,w,h);
2121 dbg(0,"%x,%x-%x,%x\n", r->lu.x,r->lu.y,r->rl.x,r->rl.y);
2122 while (scale < 1<<20) {
2124 transform_set_scale(this_->trans, scale);
2125 transform_setup_source_rect(this_->trans);
2126 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
2127 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
2128 dbg(0,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
2129 if (p1.x < 0 || p2.x < 0 || p1.x > w || p2.x > w ||
2130 p1.y < 0 || p2.y < 0 || p1.y > h || p2.y > h)
2136 dbg(0,"scale=%d (0x%x) of %d (0x%x)\n",scale,scale,1<<20,1<<20);
2137 if (this_->ready == 3)
2138 navit_draw_async(this_,0);
2142 navit_zoom_to_route(struct navit *this_, int orientation)
2145 struct map_rect *mr=NULL;
2148 struct coord_rect r;
2153 map=route_get_map(this_->route);
2154 dbg(1,"map=%p\n",map);
2156 mr=map_rect_new(map, NULL);
2157 dbg(1,"mr=%p\n",mr);
2159 while ((item=map_rect_get_item(mr))) {
2160 dbg(1,"item=%s\n", item_to_name(item->type));
2161 while (item_coord_get(item, &c, 1)) {
2166 coord_rect_extend(&r, &c);
2170 map_rect_destroy(mr);
2174 if (orientation != -1)
2175 transform_set_yaw(this_->trans, orientation);
2176 navit_zoom_to_rect(this_, &r);
2180 navit_cmd_zoom_to_route(struct navit *this)
2182 navit_zoom_to_route(this, 0);
2187 * Change the current zoom level
2189 * @param navit The navit instance
2190 * @param center The point where to center the map, including its projection
2194 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
2196 struct coord *c=transform_center(this_->trans);
2198 enum projection pro = transform_get_projection(this_->trans);
2199 if (pro != center->pro) {
2202 transform_from_to(&c1, center->pro, &c2, pro);
2209 navit_set_timeout(this_);
2210 if (this_->ready == 3)
2215 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
2219 transform_set_center(this_->trans, c);
2220 transform_get_size(this_->trans, &width, &height);
2223 update_transformation(this_->trans, &po, p, NULL);
2225 navit_set_timeout(this_);
2229 * Links all vehicles to a cursor depending on the current profile.
2231 * @param this_ A navit instance
2232 * @author Ralph Sennhauser (10/2009)
2235 navit_set_cursors(struct navit *this_)
2238 struct navit_vehicle *nv;
2242 v=g_list_first(this_->vehicles); // GList of navit_vehicles
2245 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
2246 if (!strcmp(name.u.str,"none"))
2249 c=layout_get_cursor(this_->layout_current, name.u.str);
2251 c=layout_get_cursor(this_->layout_current, "default");
2252 vehicle_set_cursor(nv->vehicle, c, 0);
2259 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
2262 struct navit_vehicle *nv=this_->vehicle;
2264 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
2265 #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 */
2266 float min_offset = 0.; // Percent offset at min_offset_speed.
2267 float max_offset = 30.; // Percent offset at max_offset_speed.
2268 int min_offset_speed = 2; // Speed in km/h
2269 int max_offset_speed = 50; // Speed ini km/h
2270 // Calculate cursor offset from the center of the screen, upon speed.
2271 if (nv->speed <= min_offset_speed) {
2272 offset = min_offset;
2273 } else if (nv->speed > max_offset_speed) {
2274 offset = max_offset;
2276 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
2280 transform_get_size(this_->trans, &width, &height);
2281 if (this_->orientation == -1 || keep_orientation) {
2283 p->y=(50 + offset)*height/100;
2285 *dir=keep_orientation?this_->orientation:nv->dir;
2288 if (this_->tracking && this_->tracking_flag) {
2289 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
2291 mdir=nv->dir-this_->orientation;
2294 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
2295 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
2297 *dir=this_->orientation;
2303 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
2307 struct navit_vehicle *nv=this_->vehicle;
2308 navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
2309 transform_set_yaw(this_->trans, dir);
2310 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
2312 navit_autozoom(this_, &nv->coord, nv->speed, 0);
2316 navit_set_center_cursor_draw(struct navit *this_)
2318 navit_set_center_cursor(this_,1,0);
2319 if (this_->ready == 3)
2320 navit_draw_async(this_, 1);
2324 navit_cmd_set_center_cursor(struct navit *this_)
2326 navit_set_center_cursor_draw(this_);
2330 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
2334 transform_reverse(this_->trans, p, &c);
2337 pc.pro = transform_get_projection(this_->trans);
2338 navit_set_center(this_, &pc, set_timeout);
2342 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
2344 int dir=0, orient_old=0, attr_updated=0;
2348 struct navit_vehicle *nv;
2351 active.type=attr_active;
2354 switch (attr->type) {
2356 attr_updated=(this_->autozoom_secs != attr->u.num);
2357 this_->autozoom_secs = attr->u.num;
2359 case attr_autozoom_active:
2360 attr_updated=(this_->autozoom_active != attr->u.num);
2361 this_->autozoom_active = attr->u.num;
2364 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
2365 dbg(1,"0x%x,0x%x\n",co.x,co.y);
2366 transform_set_center(this_->trans, &co);
2368 case attr_drag_bitmap:
2369 attr_updated=(this_->drag_bitmap != !!attr->u.num);
2370 this_->drag_bitmap=!!attr->u.num;
2373 attr_updated=(this_->flags != attr->u.num);
2374 this_->flags=attr->u.num;
2376 case attr_flags_graphics:
2377 attr_updated=(this_->graphics_flags != attr->u.num);
2378 this_->graphics_flags=attr->u.num;
2381 if (!this_->vehicle)
2383 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
2384 this_->vehicle->follow_curr = attr->u.num;
2387 if(this_->layout_current!=attr->u.layout) {
2388 this_->layout_current=attr->u.layout;
2389 graphics_font_destroy_all(this_->gra);
2390 navit_set_cursors(this_);
2391 if (this_->ready == 3)
2396 case attr_layout_name:
2400 if (!strcmp(lay->name,attr->u.str)) {
2402 attr.type=attr_layout;
2404 return navit_set_attr_do(this_, &attr, init);
2409 case attr_map_border:
2410 if (this_->border != attr->u.num) {
2411 this_->border=attr->u.num;
2415 case attr_orientation:
2416 orient_old=this_->orientation;
2417 this_->orientation=attr->u.num;
2419 if (this_->orientation != -1) {
2420 dir = this_->orientation;
2422 if (this_->vehicle) {
2423 dir = this_->vehicle->dir;
2426 transform_set_yaw(this_->trans, dir);
2427 if (orient_old != this_->orientation) {
2429 if (this_->ready == 3)
2436 case attr_osd_configuration:
2437 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
2438 attr_updated=(this_->osd_configuration != attr->u.num);
2439 this_->osd_configuration=attr->u.num;
2442 attr_updated=(this_->pitch != attr->u.num);
2443 this_->pitch=attr->u.num;
2444 transform_set_pitch(this_->trans, this_->pitch);
2445 if (!init && attr_updated && this_->ready == 3)
2448 case attr_projection:
2449 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
2450 navit_projection_set(this_, attr->u.projection, !init);
2455 attr_updated=(this_->radius != attr->u.num);
2456 this_->radius=attr->u.num;
2458 case attr_recent_dest:
2459 attr_updated=(this_->recentdest_count != attr->u.num);
2460 this_->recentdest_count=attr->u.num;
2463 if(this_->speech && this_->speech != attr->u.speech) {
2465 this_->speech = attr->u.speech;
2469 attr_updated=(this_->center_timeout != attr->u.num);
2470 this_->center_timeout = attr->u.num;
2473 attr_updated=(this_->tracking_flag != !!attr->u.num);
2474 this_->tracking_flag=!!attr->u.num;
2476 case attr_transformation:
2477 this_->trans=attr->u.transformation;
2479 case attr_use_mousewheel:
2480 attr_updated=(this_->use_mousewheel != !!attr->u.num);
2481 this_->use_mousewheel=!!attr->u.num;
2487 if (nv->vehicle == attr->u.vehicle) {
2488 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2490 vehicle_set_attr(this_->vehicle->vehicle, &active);
2492 vehicle_set_attr(nv->vehicle, &active);
2495 navit_set_vehicle(this_, nv);
2501 zoom=transform_get_scale(this_->trans);
2502 attr_updated=(zoom != attr->u.num);
2503 transform_set_scale(this_->trans, attr->u.num);
2504 if (attr_updated && !init)
2508 attr_updated=(attr->u.num != this_->zoom_min);
2509 this_->zoom_min=attr->u.num;
2512 attr_updated=(attr->u.num != this_->zoom_max);
2513 this_->zoom_max=attr->u.num;
2516 navit_add_message(this_, attr->u.str);
2518 case attr_follow_cursor:
2519 attr_updated=(this_->follow_cursor != !!attr->u.num);
2520 this_->follow_cursor=!!attr->u.num;
2523 attr_updated=(this_->imperial != attr->u.num);
2524 this_->imperial=attr->u.num;
2526 case attr_waypoints_flag:
2527 attr_updated=(this_->waypoints_flag != !!attr->u.num);
2528 this_->waypoints_flag=!!attr->u.num;
2533 if (attr_updated && !init) {
2534 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2535 if (attr->type == attr_osd_configuration)
2536 graphics_draw_mode(this_->gra, draw_mode_end);
2542 navit_set_attr(struct navit *this_, struct attr *attr)
2544 return navit_set_attr_do(this_, attr, 0);
2548 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2550 struct message *msg;
2556 msg = navit_get_messages(this_);
2564 len += strlen(msg->text) + 1;
2567 attr->u.str = g_malloc(len + 1);
2569 msg = navit_get_messages(this_);
2572 g_stpcpy((attr->u.str + offset), msg->text);
2573 offset += strlen(msg->text);
2574 attr->u.str[offset] = '\n';
2580 attr->u.str[len] = '\0';
2583 attr->u.num=this_->imperial;
2585 case attr_bookmark_map:
2586 attr->u.map=bookmarks_get_map(this_->bookmarks);
2588 case attr_bookmarks:
2589 attr->u.bookmarks=this_->bookmarks;
2591 case attr_callback_list:
2592 attr->u.callback_list=this_->attr_cbl;
2594 case attr_destination:
2595 if (! this_->destination_valid)
2597 attr->u.pcoord=&this_->destination;
2599 case attr_displaylist:
2600 attr->u.displaylist=this_->displaylist;
2601 return (attr->u.displaylist != NULL);
2603 if (!this_->vehicle)
2605 attr->u.num=this_->vehicle->follow_curr;
2607 case attr_former_destination_map:
2608 attr->u.map=this_->former_destination;
2611 attr->u.graphics=this_->gra;
2612 ret=(attr->u.graphics != NULL);
2615 attr->u.gui=this_->gui;
2616 ret=(attr->u.gui != NULL);
2621 iter->u.list=g_list_next(iter->u.list);
2623 iter->u.list=this_->layouts;
2627 attr->u.layout=(struct layout *)iter->u.list->data;
2629 attr->u.layout=this_->layout_current;
2633 if (iter && this_->mapsets) {
2634 if (!iter->u.mapset_handle) {
2635 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2637 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2639 mapset_close(iter->u.mapset_handle);
2647 attr->u.mapset=this_->mapsets->data;
2648 ret=(attr->u.mapset != NULL);
2650 case attr_navigation:
2651 attr->u.navigation=this_->navigation;
2653 case attr_orientation:
2654 attr->u.num=this_->orientation;
2657 ret=attr_generic_get_attr(this_->attr_list, NULL, type, attr, iter?(struct attr_iter *)&iter->iter:NULL);
2659 case attr_osd_configuration:
2660 attr->u.num=this_->osd_configuration;
2663 attr->u.num=transform_get_pitch(this_->trans);
2665 case attr_projection:
2667 attr->u.num=transform_get_projection(this_->trans);
2673 attr->u.route=this_->route;
2676 attr->u.speech=this_->speech;
2679 attr->u.num=this_->tracking_flag;
2681 case attr_trackingo:
2682 attr->u.tracking=this_->tracking;
2684 case attr_transformation:
2685 attr->u.transformation=this_->trans;
2690 iter->u.list=g_list_next(iter->u.list);
2692 iter->u.list=this_->vehicles;
2696 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2698 if(this_->vehicle) {
2699 attr->u.vehicle=this_->vehicle->vehicle;
2705 case attr_vehicleprofile:
2706 attr->u.vehicleprofile=this_->vehicleprofile;
2709 attr->u.num=transform_get_scale(this_->trans);
2711 case attr_autozoom_active:
2712 attr->u.num=this_->autozoom_active;
2714 case attr_follow_cursor:
2715 attr->u.num=this_->follow_cursor;
2717 case attr_waypoints_flag:
2718 attr->u.num=this_->waypoints_flag;
2728 navit_add_log(struct navit *this_, struct log *log)
2730 struct attr type_attr;
2731 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2733 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2734 char *header = "type=track_tracked\n";
2735 if (this_->textfile_debug_log)
2737 log_set_header(log, header, strlen(header));
2738 this_->textfile_debug_log=log;
2745 navit_add_layout(struct navit *this_, struct layout *layout)
2748 this_->layouts = g_list_append(this_->layouts, layout);
2749 layout_get_attr(layout, attr_active, &active, NULL);
2750 if(active.u.num || !this_->layout_current) {
2751 this_->layout_current=layout;
2758 navit_add_attr(struct navit *this_, struct attr *attr)
2761 switch (attr->type) {
2763 navit_add_callback(this_, attr->u.callback);
2766 ret=navit_add_log(this_, attr->u.log);
2769 ret=navit_set_gui(this_, attr->u.gui);
2772 ret=navit_set_graphics(this_, attr->u.graphics);
2775 ret=navit_add_layout(this_, attr->u.layout);
2778 this_->route=attr->u.route;
2781 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2783 case attr_navigation:
2784 this_->navigation=attr->u.navigation;
2787 this_->attr_list=attr_generic_add_attr(this_->attr_list, attr);
2789 case attr_recent_dest:
2790 this_->recentdest_count = attr->u.num;
2793 this_->speech=attr->u.speech;
2795 case attr_trackingo:
2796 this_->tracking=attr->u.tracking;
2799 ret=navit_add_vehicle(this_, attr->u.vehicle);
2801 case attr_vehicleprofile:
2802 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2804 case attr_autozoom_min:
2805 this_->autozoom_min = attr->u.num;
2811 this_->attrs=attr_generic_add_attr(this_->attrs, attr);
2812 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2817 navit_remove_attr(struct navit *this_, struct attr *attr)
2820 switch (attr->type) {
2822 navit_remove_callback(this_, attr->u.callback);
2825 this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
2834 navit_attr_iter_new(void)
2836 return g_new0(struct attr_iter, 1);
2840 navit_attr_iter_destroy(struct attr_iter *iter)
2846 navit_add_callback(struct navit *this_, struct callback *cb)
2848 callback_list_add(this_->attr_cbl, cb);
2852 navit_remove_callback(struct navit *this_, struct callback *cb)
2854 callback_list_remove(this_->attr_cbl, cb);
2858 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2860 * @param navit The navit instance
2865 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2867 struct point cursor_pnt;
2868 enum projection pro;
2875 pro=transform_get_projection(this_->trans_cursor);
2878 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2880 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2885 pro=transform_get_projection(this_->trans);
2886 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2889 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2891 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2897 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2899 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2900 struct pcoord cursor_pc;
2901 struct point cursor_pnt, *pnt=&cursor_pnt;
2902 struct tracking *tracking=NULL;
2903 struct pcoord pc[16];
2904 enum projection pro=transform_get_projection(this_->trans_cursor);
2906 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2908 char *destination_file;
2912 if (this_->ready != 3) {
2913 profile(0,"return 1\n");
2916 navit_layout_switch(this_);
2917 if (this_->vehicle == nv && this_->tracking_flag)
2918 tracking=this_->tracking;
2920 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2921 attr_object=tracking;
2922 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2924 attr_object=nv->vehicle;
2925 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2927 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2928 if (!attr_valid.u.num != attr_position_valid_invalid)
2930 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2931 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2932 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2933 profile(0,"return 2\n");
2936 nv->dir=*attr_dir.u.numd;
2937 nv->speed=*attr_speed.u.numd;
2938 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2939 if (nv != this_->vehicle) {
2940 navit_vehicle_draw(this_, nv, NULL);
2941 profile(0,"return 3\n");
2944 cursor_pc.x = nv->coord.x;
2945 cursor_pc.y = nv->coord.y;
2946 cursor_pc.pro = pro;
2949 route_set_position_from_tracking(this_->route, tracking, pro);
2951 route_set_position(this_->route, &cursor_pc);
2953 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2954 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2955 if (this_->gui && nv->speed > 2)
2956 navit_disable_suspend();
2958 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2959 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2960 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2961 navit_set_center_cursor_draw(this_);
2963 navit_vehicle_draw(this_, nv, pnt);
2965 if (nv->follow_curr > 1)
2968 nv->follow_curr=nv->follow;
2969 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2971 /* Finally, if we reached our destination, stop navigation. */
2973 switch(route_destination_reached(this_->route)) {
2975 description=route_get_destination_description(this_->route, 0);
2976 route_remove_waypoint(this_->route);
2977 count=route_get_destinations(this_->route, pc, 16);
2978 destination_file = bookmarks_get_destination_file(TRUE);
2979 bookmarks_append_coord(this_->former_destination, destination_file, pc, type_former_itinerary_part, description, this_->recentdest_count);
2980 g_free(description);
2983 description=route_get_destination_description(this_->route, 0);
2984 count=route_get_destinations(this_->route, pc, 1);
2985 destination_file = bookmarks_get_destination_file(TRUE);
2986 bookmarks_append_coord(this_->former_destination, destination_file, pc, type_former_itinerary_part, description, this_->recentdest_count);
2987 g_free(description);
2988 navit_set_destination(this_, NULL, NULL, 0);
2992 profile(0,"return 5\n");
2996 * Set the position of the vehicle
2998 * @param navit The navit instance
2999 * @param c The coordinate to set as position
3004 navit_set_position(struct navit *this_, struct pcoord *c)
3007 route_set_position(this_->route, c);
3008 callback_list_call_attr_0(this_->attr_cbl, attr_position);
3010 if (this_->ready == 3)
3015 navit_set_vehicleprofile(struct navit *this_, char *name)
3019 l=this_->vehicleprofiles;
3021 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
3022 if (!strcmp(attr.u.str, name)) {
3023 this_->vehicleprofile=l->data;
3025 route_set_profile(this_->route, this_->vehicleprofile);
3035 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
3039 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
3040 if (navit_set_vehicleprofile(this_, attr.u.str))
3043 if (!navit_set_vehicleprofile(this_,"car")) {
3044 /* We do not have a fallback "car" profile
3045 * so lets set any profile */
3047 l=this_->vehicleprofiles;
3049 this_->vehicleprofile=l->data;
3051 route_set_profile(this_->route, this_->vehicleprofile);
3057 * Register a new vehicle
3059 * @param navit The navit instance
3060 * @param v The vehicle instance
3061 * @returns 1 for success
3064 navit_add_vehicle(struct navit *this_, struct vehicle *v)
3066 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
3067 struct attr follow, active, animate;
3072 nv->animate_cursor=0;
3073 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
3074 nv->follow=follow.u.num;
3075 nv->follow_curr=nv->follow;
3076 this_->vehicles=g_list_append(this_->vehicles, nv);
3077 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
3078 navit_set_vehicle(this_, nv);
3079 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
3080 nv->animate_cursor=animate.u.num;
3081 nv->callback.type=attr_callback;
3082 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
3083 vehicle_add_attr(nv->vehicle, &nv->callback);
3084 vehicle_set_attr(nv->vehicle, &this_->self);
3092 navit_get_gui(struct navit *this_)
3097 struct transformation *
3098 navit_get_trans(struct navit *this_)
3100 return this_->trans;
3104 navit_get_route(struct navit *this_)
3106 return this_->route;
3110 navit_get_navigation(struct navit *this_)
3112 return this_->navigation;
3115 struct displaylist *
3116 navit_get_displaylist(struct navit *this_)
3118 return this_->displaylist;
3122 navit_layout_switch(struct navit *n)
3126 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
3127 double trise,tset,trise_actual;
3129 int year, month, day;
3131 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
3132 return; //No layout - nothing to switch
3136 l=layout_attr.u.layout;
3138 if (l->dayname || l->nightname) {
3139 //Ok, we know that we have profile to switch
3141 //Check that we aren't calculating too fast
3142 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
3143 currTs=iso8601_to_secs(iso8601_attr.u.str);
3144 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
3146 if (currTs-(n->prevTs)<60) {
3147 //We've have to wait a little
3150 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
3152 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
3153 return; //No valid fix yet
3155 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
3156 //No position - no sun
3160 //We calculate sunrise anyway, cause it is needed both for day and for night
3161 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3162 //near the pole sun never rises/sets, so we should never switch profiles
3163 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
3169 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
3172 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
3173 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
3174 //The sun is rising now!
3175 if (strcmp(l->name,l->dayname)) {
3176 navit_set_layout_by_name(n,l->dayname);
3181 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3182 //near the pole sun never rises/sets, so we should never switch profiles
3183 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
3187 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
3188 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
3189 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
3190 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
3192 if (strcmp(l->name,l->nightname)) {
3193 navit_set_layout_by_name(n,l->nightname);
3203 navit_set_vehicle_by_name(struct navit *n,const char *name)
3206 struct attr_iter *iter;
3207 struct attr vehicle_attr, name_attr;
3209 iter=navit_attr_iter_new();
3211 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
3212 v=vehicle_attr.u.vehicle;
3213 vehicle_get_attr(v,attr_name,&name_attr,NULL);
3214 if (name_attr.type==attr_name) {
3215 if (!strcmp(name,name_attr.u.str)) {
3216 navit_set_attr(n,&vehicle_attr);
3217 navit_attr_iter_destroy(iter);
3222 navit_attr_iter_destroy(iter);
3227 navit_set_layout_by_name(struct navit *n,const char *name)
3230 struct attr_iter iter;
3231 struct attr layout_attr;
3235 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
3236 return 0; //No layouts - nothing to do
3238 if (iter.u.list==NULL) {
3242 iter.u.list=g_list_first(iter.u.list);
3244 while(iter.u.list) {
3245 l=(struct layout*)iter.u.list->data;
3246 if (!strcmp(name,l->name)) {
3247 layout_attr.u.layout=l;
3248 layout_attr.type=attr_layout;
3249 navit_set_attr(n,&layout_attr);
3250 iter.u.list=g_list_first(iter.u.list);
3253 iter.u.list=g_list_next(iter.u.list);
3256 iter.u.list=g_list_first(iter.u.list);
3261 navit_disable_suspend() {
3262 gui_disable_suspend(global_navit->gui);
3263 callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
3267 navit_block(struct navit *this_, int block)
3270 this_->blocked |= 1;
3271 if (graphics_draw_cancel(this_->gra, this_->displaylist))
3272 this_->blocked |= 2;
3275 if ((this_->blocked & 2) || block < 0) {
3285 navit_destroy(struct navit *this_)
3287 dbg(0,"enter %p\n",this_);
3288 graphics_draw_cancel(this_->gra, this_->displaylist);
3289 callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
3290 attr_list_free(this_->attrs);
3291 if (this_->bookmarks) {
3292 char *center_file = bookmarks_get_center_file(TRUE);
3293 bookmarks_write_center_to_file(this_->bookmarks, center_file);
3294 g_free(center_file);
3295 bookmarks_destroy(this_->bookmarks);
3298 callback_destroy(this_->nav_speech_cb);
3299 callback_destroy(this_->roadbook_callback);
3300 callback_destroy(this_->popup_callback);
3301 callback_destroy(this_->motion_timeout_callback);
3302 callback_destroy(this_->progress_cb);
3305 graphics_remove_callback(this_->gra, this_->resize_callback);
3306 graphics_remove_callback(this_->gra, this_->button_callback);
3307 graphics_remove_callback(this_->gra, this_->motion_callback);
3308 graphics_remove_callback(this_->gra, this_->predraw_callback);
3311 callback_destroy(this_->resize_callback);
3312 callback_destroy(this_->motion_callback);
3313 callback_destroy(this_->predraw_callback);
3315 callback_destroy(this_->route_cb);
3317 route_destroy(this_->route);
3319 map_destroy(this_->former_destination);
3321 graphics_displaylist_destroy(this_->displaylist);
3323 graphics_free(this_->gra);
3329 navit_ref(struct navit *this_)
3332 dbg(0,"refcount %d\n",this_->refcount);
3337 navit_unref(struct navit *this_)
3340 dbg(0,"refcount %d\n",this_->refcount);
3341 if (this_->refcount <= 0)
3342 navit_destroy(this_);
3345 struct object_func navit_func = {
3347 (object_func_new)navit_new,
3348 (object_func_get_attr)navit_get_attr,
3349 (object_func_iter_new)navit_attr_iter_new,
3350 (object_func_iter_destroy)navit_attr_iter_destroy,
3351 (object_func_set_attr)navit_set_attr,
3352 (object_func_add_attr)navit_add_attr,
3353 (object_func_remove_attr)navit_remove_attr,
3354 (object_func_init)navit_init,
3355 (object_func_destroy)navit_destroy,
3356 (object_func_dup)NULL,
3357 (object_func_ref)navit_ref,
3358 (object_func_unref)navit_unref,