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 */
166 struct attr **attr_list;
169 struct gui *main_loop_gui;
175 struct mapset_handle *mapset_handle;
179 static void navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv);
180 static void navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt);
181 static int navit_add_vehicle(struct navit *this_, struct vehicle *v);
182 static int navit_set_attr_do(struct navit *this_, struct attr *attr, int init);
183 static int navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir);
184 static void navit_set_cursors(struct navit *this_);
185 static void navit_cmd_zoom_to_route(struct navit *this);
186 static void navit_cmd_set_center_cursor(struct navit *this_);
187 static void navit_cmd_announcer_toggle(struct navit *this_);
188 static void navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv);
189 struct object_func navit_func;
191 struct navit *global_navit;
194 navit_add_mapset(struct navit *this_, struct mapset *ms)
196 this_->mapsets = g_list_append(this_->mapsets, ms);
200 navit_get_mapset(struct navit *this_)
203 return this_->mapsets->data;
205 dbg(0,"No mapsets enabled! Is it on purpose? Navit can't draw a map. Please check your navit.xml\n");
211 navit_get_tracking(struct navit *this_)
213 return this_->tracking;
217 * @brief Get the user data directory.
218 * @param[in] create - create the directory if it does not exist
220 * @return char * to the data directory string.
222 * returns the directory used to store user data files (center.txt,
223 * destination.txt, bookmark.txt, ...)
227 navit_get_user_data_directory(int create) {
229 dir = getenv("NAVIT_USER_DATADIR");
230 if (create && !file_exists(dir)) {
231 dbg(0,"creating dir %s\n", dir);
232 if (file_mkdir(dir,0)) {
233 dbg(0,"failed creating dir %s\n", dir);
238 } /* end: navit_get_user_data_directory(gboolean create) */
242 navit_draw_async(struct navit *this_, int async)
245 if (this_->blocked) {
249 transform_setup_source_rect(this_->trans);
250 graphics_draw(this_->gra, this_->displaylist, this_->mapsets->data, this_->trans, this_->layout_current, async, NULL, this_->graphics_flags|1);
254 navit_draw(struct navit *this_)
256 if (this_->ready == 3)
257 navit_draw_async(this_, 0);
261 navit_get_ready(struct navit *this_)
269 navit_draw_displaylist(struct navit *this_)
271 if (this_->ready == 3)
272 graphics_displaylist_draw(this_->gra, this_->displaylist, this_->trans, this_->layout_current, this_->graphics_flags|1);
276 navit_map_progress(struct navit *this_)
280 struct mapset_handle *msh;
283 if (this_->ready != 3)
288 ms=this_->mapsets->data;
290 while (msh && (map=mapset_next(msh, 0))) {
291 if (map_get_attr(map, attr_progress, &attr, NULL)) {
292 char *str=g_strdup_printf("%s ",attr.u.str);
293 graphics_draw_mode(this_->gra, draw_mode_begin);
294 graphics_draw_text_std(this_->gra, 16, str, &p);
297 graphics_draw_mode(this_->gra, draw_mode_end);
304 navit_redraw_route(struct navit *this_, struct route *route, struct attr *attr)
307 if (attr->type != attr_route_status)
310 if (this_->ready != 3)
312 if (updated != route_status_path_done_new)
314 if (this_->vehicle) {
315 if (this_->vehicle->follow_curr == 1)
317 if (this_->vehicle->follow_curr <= this_->vehicle->follow)
318 this_->vehicle->follow_curr=this_->vehicle->follow;
324 navit_handle_resize(struct navit *this_, int w, int h)
326 struct map_selection sel;
327 int callback=(this_->ready == 1);
329 memset(&sel, 0, sizeof(sel));
334 transform_set_screen_selection(this_->trans, &sel);
335 graphics_init(this_->gra);
336 graphics_set_rect(this_->gra, &sel.u.p_rect);
338 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
339 if (this_->ready == 3)
340 navit_draw_async(this_, 1);
344 navit_resize(void *data, int w, int h)
346 struct navit *this=data;
347 if (!this->ignore_graphics_events)
348 navit_handle_resize(this, w, h);
352 navit_get_width(struct navit *this_)
359 navit_get_height(struct navit *this_)
365 navit_popup(void *data)
367 struct navit *this_=data;
368 popup(this_, 1, &this_->pressed);
369 this_->button_timeout=NULL;
375 navit_ignore_button(struct navit *this_)
377 if (this_->ignore_button)
379 this_->ignore_button=1;
384 navit_ignore_graphics_events(struct navit *this_, int ignore)
386 this_->ignore_graphics_events=ignore;
390 update_transformation(struct transformation *tr, struct point *old, struct point *new, struct point *rot)
395 double angleo,anglen;
397 if (!transform_reverse(tr, old, &co))
400 angleo=atan2(old->y-rot->y, old->x-rot->x)*180/M_PI;
401 anglen=atan2(new->y-rot->y, new->x-rot->x)*180/M_PI;
402 yaw=transform_get_yaw(tr)+angleo-anglen;
403 transform_set_yaw(tr, yaw % 360);
405 if (!transform_reverse(tr, new, &cn))
407 cp=transform_get_center(tr);
410 dbg(1,"from 0x%x,0x%x to 0x%x,0x%x\n", cp->x, cp->y, c.x, c.y);
411 transform_set_center(tr, &c);
415 navit_set_timeout(struct navit *this_)
418 follow.type=attr_follow;
419 follow.u.num=this_->center_timeout;
420 navit_set_attr(this_, &follow);
424 navit_handle_button(struct navit *this_, int pressed, int button, struct point *p, struct callback *popup_callback)
428 dbg(1,"enter %d %d (ignore %d)\n",pressed,button,this_->ignore_button);
429 callback_list_call_attr_4(this_->attr_cbl, attr_button, this_, GINT_TO_POINTER(pressed), GINT_TO_POINTER(button), p);
430 if (this_->ignore_button) {
431 this_->ignore_button=0;
439 this_->button_pressed=1;
443 this_->button_timeout=event_add_timeout(500, 0, popup_callback);
446 navit_set_center_screen(this_, p, 1);
448 popup(this_, button, p);
449 if (button == 4 && this_->use_mousewheel) {
451 navit_zoom_in(this_, 2, p);
453 if (button == 5 && this_->use_mousewheel) {
455 navit_zoom_out(this_, 2, p);
459 this_->button_pressed=0;
460 if (this_->button_timeout) {
461 event_remove_timeout(this_->button_timeout);
462 this_->button_timeout=NULL;
463 if (! this_->moved && ! transform_within_border(this_->trans, p, border)) {
464 navit_set_center_screen(this_, p, !this_->zoomed);
467 if (this_->motion_timeout) {
468 event_remove_timeout(this_->motion_timeout);
469 this_->motion_timeout=NULL;
476 update_transformation(this_->trans, &this_->pressed, p, &pr);
478 update_transformation(this_->trans, &this_->pressed, p, NULL);
480 graphics_draw_drag(this_->gra, NULL);
481 transform_copy(this_->trans, this_->trans_cursor);
482 graphics_overlay_disable(this_->gra, 0);
484 navit_set_timeout(this_);
493 navit_button(void *data, int pressed, int button, struct point *p)
495 struct navit *this=data;
496 dbg(1,"enter %d %d ignore %d\n",pressed,button,this->ignore_graphics_events);
497 if (!this->ignore_graphics_events) {
498 if (! this->popup_callback)
499 this->popup_callback=callback_new_1(callback_cast(navit_popup), this);
500 navit_handle_button(this, pressed, button, p, this->popup_callback);
506 navit_motion_timeout(struct navit *this_)
510 if (this_->drag_bitmap) {
512 point.x=(this_->current.x-this_->pressed.x);
513 point.y=(this_->current.y-this_->pressed.y);
514 if (graphics_draw_drag(this_->gra, &point)) {
515 graphics_overlay_disable(this_->gra, 1);
516 graphics_draw_mode(this_->gra, draw_mode_end);
518 this_->motion_timeout=NULL;
522 dx=(this_->current.x-this_->last.x);
523 dy=(this_->current.y-this_->last.y);
525 struct transformation *tr;
527 this_->last=this_->current;
528 graphics_overlay_disable(this_->gra, 1);
529 tr=transform_dup(this_->trans);
533 update_transformation(tr, &this_->pressed, &this_->current, &pr);
535 update_transformation(tr, &this_->pressed, &this_->current, NULL);
538 graphics_displaylist_move(this_->displaylist, dx, dy);
540 graphics_draw_cancel(this_->gra, this_->displaylist);
541 graphics_displaylist_draw(this_->gra, this_->displaylist, tr, this_->layout_current, this_->graphics_flags);
542 transform_destroy(tr);
545 this_->motion_timeout=NULL;
550 navit_handle_motion(struct navit *this_, struct point *p)
554 if (this_->button_pressed && !this_->popped) {
555 dx=(p->x-this_->pressed.x);
556 dy=(p->y-this_->pressed.y);
557 if (dx < -8 || dx > 8 || dy < -8 || dy > 8) {
559 if (this_->button_timeout) {
560 event_remove_timeout(this_->button_timeout);
561 this_->button_timeout=NULL;
564 if (! this_->motion_timeout_callback)
565 this_->motion_timeout_callback=callback_new_1(callback_cast(navit_motion_timeout), this_);
566 if (! this_->motion_timeout)
567 this_->motion_timeout=event_add_timeout(100, 0, this_->motion_timeout_callback);
573 navit_motion(void *data, struct point *p)
575 struct navit *this=data;
576 if (!this->ignore_graphics_events)
577 navit_handle_motion(this, p);
581 navit_predraw(struct navit *this_)
584 struct navit_vehicle *nv;
585 transform_copy(this_->trans, this_->trans_cursor);
589 navit_vehicle_draw(this_, nv, NULL);
595 navit_scale(struct navit *this_, long scale, struct point *p, int draw)
597 struct coord c1, c2, *center;
598 if (scale < this_->zoom_min)
599 scale=this_->zoom_min;
600 if (scale > this_->zoom_max)
601 scale=this_->zoom_max;
603 transform_reverse(this_->trans, p, &c1);
604 transform_set_scale(this_->trans, scale);
606 transform_reverse(this_->trans, p, &c2);
607 center = transform_center(this_->trans);
608 center->x += c1.x - c2.x;
609 center->y += c1.y - c2.y;
616 * @brief Automatically adjusts zoom level
618 * This function automatically adjusts the current
619 * zoom level according to the current speed.
621 * @param this_ The navit struct
622 * @param center The "immovable" point - i.e. the vehicles position if we're centering on the vehicle
623 * @param speed The vehicles speed in meters per second
624 * @param dir The direction into which the vehicle moves
627 navit_autozoom(struct navit *this_, struct coord *center, int speed, int draw)
634 if (! this_->autozoom_active) {
638 distance = speed * this_->autozoom_secs;
640 transform_get_size(this_->trans, &w, &h);
641 transform(this_->trans, transform_get_projection(this_->trans), center, &pc, 1, 0, 0, NULL);
642 scale = transform_get_scale(this_->trans);
644 /* We make sure that the point we want to see is within a certain range
645 * around the vehicle. The radius of this circle is the size of the
646 * screen. This doesn't necessarily mean the point is visible because of
647 * perspective etc. Quite rough, but should be enough. */
650 new_scale = (double)distance / h * 16;
652 new_scale = (double)distance / w * 16;
655 if (abs(new_scale - scale) < 2) {
659 if (new_scale >= this_->autozoom_min) {
660 navit_scale(this_, (long)new_scale, &pc, 0);
662 if (scale != this_->autozoom_min) {
663 navit_scale(this_, this_->autozoom_min, &pc, 0);
669 * Change the current zoom level, zooming closer to the ground
671 * @param navit The navit instance
672 * @param factor The zoom factor, usually 2
673 * @param p The invariant point (if set to NULL, default to center)
677 navit_zoom_in(struct navit *this_, int factor, struct point *p)
679 long scale=transform_get_scale(this_->trans)/factor;
682 navit_scale(this_, scale, p, 1);
686 * Change the current zoom level
688 * @param navit The navit instance
689 * @param factor The zoom factor, usually 2
690 * @param p The invariant point (if set to NULL, default to center)
694 navit_zoom_out(struct navit *this_, int factor, struct point *p)
696 long scale=transform_get_scale(this_->trans)*factor;
697 navit_scale(this_, scale, p, 1);
701 navit_zoom_in_cursor(struct navit *this_, int factor)
704 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
705 navit_zoom_in(this_, factor, &p);
706 this_->vehicle->follow_curr=this_->vehicle->follow;
708 navit_zoom_in(this_, factor, NULL);
712 navit_zoom_out_cursor(struct navit *this_, int factor)
715 if (this_->vehicle && this_->vehicle->follow_curr <= 1 && navit_get_cursor_pnt(this_, &p, 0, NULL)) {
716 navit_zoom_out(this_, 2, &p);
717 this_->vehicle->follow_curr=this_->vehicle->follow;
719 navit_zoom_out(this_, 2, NULL);
723 navit_cmd_zoom_in(struct navit *this_)
725 navit_zoom_in_cursor(this_, 2);
730 navit_cmd_zoom_out(struct navit *this_)
732 navit_zoom_out_cursor(this_, 2);
738 navit_cmd_say(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
740 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str)
741 navit_say(this, in[0]->u.str);
744 static GHashTable *cmd_int_var_hash = NULL;
745 static GHashTable *cmd_attr_var_hash = NULL;
748 * Store key value pair for the command system (for int typed values)
750 * @param navit The navit instance
751 * @param function unused (needed to match command function signiture)
752 * @param in input attributes in[0] is the key string, in[1] is the integer value to store
753 * @param out output attributes, unused
754 * @param valid unused
758 navit_cmd_set_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
762 if(!cmd_int_var_hash) {
763 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
766 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
767 (in && in[1] && ATTR_IS_NUMERIC(in[1]->type))) {
768 val = g_new(struct attr,1);
769 attr_dup_content(in[1],val);
770 key = g_strdup(in[0]->u.str);
771 g_hash_table_insert(cmd_int_var_hash, key, val);
777 * Store key value pair for the command system (for attr typed values, can be used as opaque handles)
779 * @param navit The navit instance
780 * @param function unused (needed to match command function signiture)
781 * @param in input attributes in[0] is the key string, in[1] is the attr* value to store
782 * @param out output attributes, unused
783 * @param valid unused
786 //TODO free stored attributes on navit_destroy
788 navit_cmd_set_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
792 if(!cmd_attr_var_hash) {
793 cmd_attr_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
796 if ( (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) &&
798 val = attr_dup(in[1]);
800 key = g_strdup(in[0]->u.str);
801 g_hash_table_insert(cmd_attr_var_hash, key, val);
803 dbg(1, "Wrong parameters for set_attr_var() command function\n");
810 * command to toggle the active state of a named layer of the current layout
812 * @param navit The navit instance
813 * @param function unused (needed to match command function signiture)
814 * @param in input attribute in[0] is the name of the layer
815 * @param out output unused
816 * @param valid unused
820 navit_cmd_toggle_layer(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
822 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
823 if(this->layout_current && this->layout_current->layers) {
824 GList* layers = this->layout_current->layers;
826 struct layer*l=layers->data;
827 if(l && !strcmp(l->name,in[0]->u.str) ) {
832 layers=g_list_next(layers);
839 * adds an item with the current coordinate of the vehicle to a named map
841 * @param navit The navit instance
842 * @param function unused (needed to match command function signiture)
843 * @param in input attribute in[0] is the name of the map
844 * @param out output attribute, 0 on error or the id of the created item on success
845 * @param valid unused
849 navit_cmd_map_add_curr_pos(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
851 struct attr **list = g_new0(struct attr *,2);
852 struct attr*val = g_new0(struct attr,1);
854 struct map_selection sel;
855 const int selection_range = 10;
856 enum item_type item_type;
858 struct map* curr_map = NULL;
859 struct coord curr_coord;
862 //return invalid item on error
863 val->type = attr_none;
869 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str && //map name
870 in[1] && ATTR_IS_STRING(in[1]->type) && in[1]->u.str //item type
873 if(!(ms=navit_get_mapset(this))) {
874 dbg(1, "Command function map_add_curr_pos(): there is no active mapset\n");
878 if((item_type = item_from_name(in[1]->u.str))==type_none) {
879 dbg(1, "Command function map_add_curr_pos(): unknown item type\n");
883 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
885 //no map with the given name found
887 dbg(1, "Command function map_add_curr_pos(): map not found\n");
891 if(this->vehicle && this->vehicle->vehicle ) {
892 struct attr pos_attr;
893 if(vehicle_get_attr(this->vehicle->vehicle,attr_position_coord_geo,&pos_attr,NULL)) {
894 transform_from_geo(projection_mg, pos_attr.u.coord_geo, &curr_coord);
896 dbg(1, "Command function map_add_curr_pos(): vehicle position is not accessible\n");
900 dbg(1, "Command function map_add_curr_pos(): no vehicle\n");
906 sel.range.min=type_none;
907 sel.range.max=type_tec_common;
908 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
909 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
910 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
911 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
913 mr = map_rect_new(curr_map, &sel);
915 it = map_rect_create_item( mr, item_type);
917 val->type = attr_type_item_begin;
918 item_coord_set(it,&curr_coord, 1, change_mode_modify);
922 map_rect_destroy(mr);
927 * sets an attribute (name value pair) of a map item specified by map name and item id
929 * @param navit The navit instance
930 * @param function unused (needed to match command function signiture)
931 * @param in input attribute in[0] - name of the map ; in[1] - item ; in[2] - attr name ; in[3] - attr value
932 * @param out output attribute, 0 on error, 1 on success
933 * @param valid unused
937 navit_cmd_map_item_set_attr(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
940 in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str &&//map name
941 in[1] && ATTR_IS_ITEM(in[1]->type) && //item
942 in[2] && ATTR_IS_STRING(in[2]->type) && in[2]->u.str && //attr_type str
943 in[3] && ATTR_IS_STRING(in[3]->type) && in[3]->u.str //attr_value str
945 struct attr attr_to_set;
946 struct map* curr_map = NULL;
948 struct map_selection sel;
949 const int selection_range = 500;
950 struct coord curr_coord;
953 if(ATTR_IS_STRING(attr_from_name(in[2]->u.str))) {
954 attr_to_set.u.str = in[3]->u.str;
955 attr_to_set.type = attr_from_name(in[2]->u.str);
957 else if(ATTR_IS_INT(attr_from_name(in[2]->u.str))) {
958 attr_to_set.u.num = atoi(in[3]->u.str);
959 attr_to_set.type = attr_from_name(in[2]->u.str);
961 else if(ATTR_IS_DOUBLE(attr_from_name(in[2]->u.str))) {
962 double* val = g_new0(double,1);
963 *val = atof(in[3]->u.str);
964 attr_to_set.u.numd = val;
965 attr_to_set.type = attr_from_name(in[2]->u.str);
968 ms = navit_get_mapset(this);
970 curr_map = mapset_get_map_by_name(ms, in[0]->u.str);
977 sel.range.min=type_none;
978 sel.range.max=type_tec_common;
979 sel.u.c_rect.lu.x=curr_coord.x-selection_range;
980 sel.u.c_rect.lu.y=curr_coord.y+selection_range;
981 sel.u.c_rect.rl.x=curr_coord.x+selection_range;
982 sel.u.c_rect.rl.y=curr_coord.y-selection_range;
986 item_attr_set(it, &attr_to_set, change_mode_modify);
989 dbg(4,"Error in command function item_set_attr()\n");
990 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);
991 dbg(4,"Command function item_set_attr(): item cond: %d\n",(in[1] && ATTR_IS_ITEM(in[1]->type))?1:0);
992 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);
993 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);
998 * Get attr variable given a key string for the command system (for opaque usage)
1000 * @param navit The navit instance
1001 * @param function unused (needed to match command function signiture)
1002 * @param in input attribute in[0] is the key string
1003 * @param out output attribute, the attr for the given key string if exists or NULL
1004 * @param valid unused
1008 navit_cmd_get_attr_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1010 struct attr **list = g_new0(struct attr *,2);
1011 if(!cmd_attr_var_hash) {
1012 struct attr*val = g_new0(struct attr,1);
1013 cmd_attr_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
1014 val->type = attr_type_item_begin;
1018 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
1019 struct attr*ret = g_hash_table_lookup(cmd_attr_var_hash, in[0]->u.str);
1021 list[0] = attr_dup(ret);
1024 struct attr*val = g_new0(struct attr,1);
1025 val->type = attr_type_int_begin;
1036 * Get value given a key string for the command system
1038 * @param navit The navit instance
1039 * @param function unused (needed to match command function signiture)
1040 * @param in input attribute in[0] is the key string
1041 * @param out output attribute, the value for the given key string if exists or 0
1042 * @param valid unused
1046 navit_cmd_get_int_var(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1048 struct attr **list = g_new0(struct attr *,2);
1049 if(!cmd_int_var_hash) {
1050 struct attr*val = g_new0(struct attr,1);
1051 cmd_int_var_hash = g_hash_table_new(g_str_hash, g_str_equal);
1052 val->type = attr_type_int_begin;
1056 if (in && in[0] && ATTR_IS_STRING(in[0]->type) && in[0]->u.str) {
1057 struct attr*ret = g_hash_table_lookup(cmd_int_var_hash, in[0]->u.str);
1059 list[0] = attr_dup(ret);
1062 struct attr*val = g_new0(struct attr,1);
1063 val->type = attr_type_int_begin;
1072 GList *cmd_int_var_stack = NULL;
1075 * Push an integer to the stack for the command system
1077 * @param navit The navit instance
1078 * @param function unused (needed to match command function signiture)
1079 * @param in input attribute in[0] is the integer attibute to push
1080 * @param out output attributes, unused
1081 * @param valid unused
1085 navit_cmd_push_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1087 if (in && in[0] && ATTR_IS_NUMERIC(in[0]->type)) {
1088 struct attr*val = g_new(struct attr,1);
1089 attr_dup_content(in[0],val);
1090 cmd_int_var_stack = g_list_prepend(cmd_int_var_stack, val);
1095 * Pop an integer from the command system's integer stack
1097 * @param navit The navit instance
1098 * @param function unused (needed to match command function signiture)
1099 * @param in input attributes unused
1100 * @param out output attribute, the value popped if stack isn't empty or 0
1101 * @param valid unused
1105 navit_cmd_pop_int(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1107 struct attr **list = g_new0(struct attr *,2);
1108 if(!cmd_int_var_stack) {
1109 struct attr*val = g_new0(struct attr,1);
1110 val->type = attr_type_int_begin;
1115 list[0] = cmd_int_var_stack->data;
1116 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1123 * Get current size of command system's integer stack
1125 * @param navit The navit instance
1126 * @param function unused (needed to match command function signiture)
1127 * @param in input attributes unused
1128 * @param out output attribute, the size of stack
1129 * @param valid unused
1133 navit_cmd_int_stack_size(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1136 struct attr *attr = g_new0(struct attr ,1);
1137 attr->type = attr_type_int_begin;
1138 if(!cmd_int_var_stack) {
1142 attr->u.num = g_list_length(cmd_int_var_stack);
1144 list = g_new0(struct attr *,2);
1148 cmd_int_var_stack = g_list_remove_link(cmd_int_var_stack,cmd_int_var_stack);
1151 static struct attr **
1152 navit_get_coord(struct navit *this, struct attr **in, struct pcoord *pc)
1158 pc->pro = transform_get_projection(this->trans);
1159 if (ATTR_IS_STRING(in[0]->type)) {
1161 coord_parse(in[0]->u.str, pc->pro, &c);
1165 } else if (ATTR_IS_COORD(in[0]->type)) {
1166 pc->x=in[0]->u.coord->x;
1167 pc->y=in[0]->u.coord->y;
1169 } else if (ATTR_IS_PCOORD(in[0]->type)) {
1170 *pc=*in[0]->u.pcoord;
1172 } else if (in[1] && in[2] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type) && ATTR_IS_INT(in[2]->type)) {
1173 pc->pro=in[0]->u.num;
1177 } else if (in[1] && ATTR_IS_INT(in[0]->type) && ATTR_IS_INT(in[1]->type)) {
1187 navit_cmd_set_destination(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1190 char *description=NULL;
1191 in=navit_get_coord(this, in, &pc);
1194 if (in[0] && ATTR_IS_STRING(in[0]->type))
1195 description=in[0]->u.str;
1196 navit_set_destination(this, &pc, description, 1);
1201 navit_cmd_set_center(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1204 in=navit_get_coord(this, in, &pc);
1207 navit_set_center(this, &pc, 0);
1212 navit_cmd_set_position(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1215 in=navit_get_coord(this, in, &pc);
1218 navit_set_position(this, &pc);
1223 navit_cmd_fmt_coordinates(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1226 attr.type=attr_type_string_begin;
1227 attr.u.str="Fix me";
1229 *out=attr_generic_add_attr(*out, &attr);
1234 * Join several string attributes into one
1236 * @param navit The navit instance
1237 * @param function unused (needed to match command function signiture)
1238 * @param in input attributes in[0] - separator, in[1..] - attributes to join
1239 * @param out output attribute joined attribute as string
1240 * @param valid unused
1244 navit_cmd_strjoin(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1249 attr.type=attr_type_string_begin;
1251 if(in[0] && in[1]) {
1252 sep=attr_to_text(in[0],NULL,1);
1253 ret=attr_to_text(in[1],NULL,1);
1254 for(i=2;in[i];i++) {
1255 gchar *in_i=attr_to_text(in[i],NULL,1);
1256 gchar *r=g_strjoin(sep,ret,in_i,NULL);
1264 *out=attr_generic_add_attr(*out, &attr);
1271 * Call external program
1273 * @param navit The navit instance
1274 * @param function unused (needed to match command function signiture)
1275 * @param in input attributes in[0] - name of executable, in[1..] - parameters
1276 * @param out output attribute unused
1277 * @param valid unused
1281 navit_cmd_spawn(struct navit *this, char *function, struct attr **in, struct attr ***out, int *valid)
1283 int i,j, nparms, nvalid;
1284 const char ** argv=NULL;
1285 struct spawn_process_info *pi;
1291 if (in[nparms]->type!=attr_none)
1298 argv=g_new(char*,nvalid+1);
1299 for(i=0,j=0;in[i];i++) {
1300 if(in[i]->type!=attr_none ) {
1301 argv[j++]=attr_to_text(in[i],NULL,1);
1303 dbg(0,"Parameter #%i is attr_none - skipping\n",i);
1307 pi=spawn_process(argv);
1309 // spawn_process() testing suite - uncomment following code to test.
1311 // example of non-blocking wait
1312 //int st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1313 // example of blocking wait
1314 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1315 // example of wait after process is finished and status is
1317 //st=spawn_process_check_status(pi,1);dbg(0,"status %i\n",st);
1318 // example of wait after process is finished and status is
1319 // already tested - unblocked
1320 //st=spawn_process_check_status(pi,0);dbg(0,"status %i\n",st);
1322 // End testing suite
1323 spawn_process_info_free(pi);
1324 for(i=0;argv[i];i++)
1331 static struct command_table commands[] = {
1332 {"zoom_in",command_cast(navit_cmd_zoom_in)},
1333 {"zoom_out",command_cast(navit_cmd_zoom_out)},
1334 {"zoom_to_route",command_cast(navit_cmd_zoom_to_route)},
1335 {"say",command_cast(navit_cmd_say)},
1336 {"set_center",command_cast(navit_cmd_set_center)},
1337 {"set_center_cursor",command_cast(navit_cmd_set_center_cursor)},
1338 {"set_destination",command_cast(navit_cmd_set_destination)},
1339 {"set_position",command_cast(navit_cmd_set_position)},
1340 {"announcer_toggle",command_cast(navit_cmd_announcer_toggle)},
1341 {"fmt_coordinates",command_cast(navit_cmd_fmt_coordinates)},
1342 {"set_int_var",command_cast(navit_cmd_set_int_var)},
1343 {"get_int_var",command_cast(navit_cmd_get_int_var)},
1344 {"push_int",command_cast(navit_cmd_push_int)},
1345 {"pop_int",command_cast(navit_cmd_pop_int)},
1346 {"int_stack_size",command_cast(navit_cmd_int_stack_size)},
1347 {"toggle_layer",command_cast(navit_cmd_toggle_layer)},
1348 {"strjoin",command_cast(navit_cmd_strjoin)},
1349 {"spawn",command_cast(navit_cmd_spawn)},
1350 {"map_add_curr_pos",command_cast(navit_cmd_map_add_curr_pos)},
1351 {"map_item_set_attr",command_cast(navit_cmd_map_item_set_attr)},
1352 {"set_attr_var",command_cast(navit_cmd_set_attr_var)},
1353 {"get_attr_var",command_cast(navit_cmd_get_attr_var)},
1357 navit_command_add_table(struct navit*this_, struct command_table *commands, int count)
1359 command_add_table(this_->attr_cbl, commands, count, this_);
1363 navit_new(struct attr *parent, struct attr **attrs)
1365 struct navit *this_=g_new0(struct navit, 1);
1366 struct pcoord center;
1369 enum projection pro=projection_mg;
1374 this_->func=&navit_func;
1376 this_->attrs=attr_list_dup(attrs);
1377 this_->self.type=attr_navit;
1378 this_->self.u.navit=this_;
1379 this_->attr_cbl=callback_list_new();
1381 this_->orientation=-1;
1382 this_->tracking_flag=1;
1383 this_->recentdest_count=10;
1384 this_->osd_configuration=-1;
1386 this_->center_timeout = 10;
1387 this_->use_mousewheel = 1;
1388 this_->autozoom_secs = 10;
1389 this_->autozoom_min = 7;
1390 this_->autozoom_active = 0;
1391 this_->zoom_min = 1;
1392 this_->zoom_max = 2097152;
1393 this_->follow_cursor = 1;
1397 this_->trans = transform_new();
1398 this_->trans_cursor = transform_new();
1399 transform_from_geo(pro, &g, &co);
1404 transform_setup(this_->trans, ¢er, zoom, (this_->orientation != -1) ? this_->orientation : 0);
1406 this_->bookmarks=bookmarks_new(&this_->self, NULL, this_->trans);
1410 for (;*attrs; attrs++) {
1411 navit_set_attr_do(this_, *attrs, 1);
1413 this_->displaylist=graphics_displaylist_new();
1414 command_add_table(this_->attr_cbl, commands, sizeof(commands)/sizeof(struct command_table), this_);
1416 this_->messages = messagelist_new(attrs);
1418 dbg(0,"return %p\n",this_);
1424 navit_set_gui(struct navit *this_, struct gui *gui)
1429 if (gui_has_main_loop(this_->gui)) {
1430 if (! main_loop_gui) {
1431 main_loop_gui=this_->gui;
1433 dbg(0,"gui with main loop already active, ignoring this instance");
1441 navit_add_message(struct navit *this_, char *message)
1443 message_new(this_->messages, message);
1447 *navit_get_messages(struct navit *this_)
1449 return message_get(this_->messages);
1453 navit_set_graphics(struct navit *this_, struct graphics *gra)
1458 this_->resize_callback=callback_new_attr_1(callback_cast(navit_resize), attr_resize, this_);
1459 graphics_add_callback(gra, this_->resize_callback);
1460 this_->button_callback=callback_new_attr_1(callback_cast(navit_button), attr_button, this_);
1461 graphics_add_callback(gra, this_->button_callback);
1462 this_->motion_callback=callback_new_attr_1(callback_cast(navit_motion), attr_motion, this_);
1463 graphics_add_callback(gra, this_->motion_callback);
1464 this_->predraw_callback=callback_new_attr_1(callback_cast(navit_predraw), attr_predraw, this_);
1465 graphics_add_callback(gra, this_->predraw_callback);
1470 navit_get_graphics(struct navit *this_)
1475 struct vehicleprofile *
1476 navit_get_vehicleprofile(struct navit *this_)
1478 return this_->vehicleprofile;
1482 navit_get_vehicleprofiles(struct navit *this_)
1484 return this_->vehicleprofiles;
1488 navit_projection_set(struct navit *this_, enum projection pro, int draw)
1493 c=transform_center(this_->trans);
1494 transform_to_geo(transform_get_projection(this_->trans), c, &g);
1495 transform_set_projection(this_->trans, pro);
1496 transform_from_geo(pro, &g, c);
1502 navit_mark_navigation_stopped(char *former_destination_file){
1504 f=fopen(former_destination_file, "a");
1506 fprintf(f,"%s", TEXTFILE_COMMENT_NAVI_STOPPED);
1509 dbg(0, "Error setting mark in destination file %s: %s\n", former_destination_file, strerror(errno));
1514 * Start the route computing to a given set of coordinates
1516 * @param navit The navit instance
1517 * @param c The coordinate to start routing to
1518 * @param description A label which allows the user to later identify this destination in the former destinations selection
1522 navit_set_destination(struct navit *this_, struct pcoord *c, const char *description, int async)
1524 char *destination_file;
1525 destination_file = bookmarks_get_destination_file(TRUE);
1527 this_->destination=*c;
1528 this_->destination_valid=1;
1530 dbg(1, "c=(%i,%i)\n", c->x,c->y);
1531 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_destination, description, this_->recentdest_count);
1533 this_->destination_valid=0;
1534 navit_mark_navigation_stopped(destination_file);
1536 g_free(destination_file);
1537 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1539 route_set_destination(this_->route, c, async);
1541 if (this_->ready == 3)
1547 * Start the route computing to a given set of coordinates including waypoints
1549 * @param navit The navit instance
1550 * @param c The coordinate to start routing to
1551 * @param description A label which allows the user to later identify this destination in the former destinations selection
1555 navit_set_destinations(struct navit *this_, struct pcoord *c, int count, const char *description, int async)
1557 char *destination_file;
1559 this_->destination=c[count-1];
1560 this_->destination_valid=1;
1562 destination_file = bookmarks_get_destination_file(TRUE);
1563 bookmarks_append_coord(this_->former_destination, destination_file, c, type_former_itinerary, description, this_->recentdest_count);
1564 g_free(destination_file);
1566 this_->destination_valid=0;
1567 callback_list_call_attr_0(this_->attr_cbl, attr_destination);
1569 route_set_destinations(this_->route, c, count, async);
1571 if (this_->ready == 3)
1577 * @brief Checks if a route is calculated
1579 * This function checks if a route is calculated.
1581 * @param this_ The navit struct whose route should be checked.
1582 * @return True if the route is set, false otherwise.
1585 navit_check_route(struct navit *this_)
1588 return route_get_path_set(this_->route);
1595 navit_former_destinations_active(struct navit *this_)
1597 char *destination_file_name = bookmarks_get_destination_file(FALSE);
1598 FILE *destination_file;
1601 destination_file=fopen(destination_file_name,"r");
1602 if (destination_file) {
1603 while(fgets(lastline, sizeof(lastline), destination_file));
1604 fclose(destination_file);
1605 if ((lastline != NULL) && (strcmp(lastline, TEXTFILE_COMMENT_NAVI_STOPPED))){
1609 g_free(destination_file_name);
1613 struct map* read_former_destinations_from_file(){
1614 struct attr type, data, flags, *attrs[4];
1615 char *destination_file = bookmarks_get_destination_file(FALSE);
1618 type.type=attr_type;
1619 type.u.str="textfile";
1621 data.type=attr_data;
1622 data.u.str=destination_file;
1624 flags.type=attr_flags;
1627 attrs[0]=&type; attrs[1]=&data; attrs[2]=&flags; attrs[3]=NULL;
1629 m=map_new(NULL, attrs);
1630 g_free(destination_file);
1635 navit_add_former_destinations_from_file(struct navit *this_)
1638 int i,valid=0,count=0;
1640 struct pcoord pc[16];
1641 struct map_rect *mr;
1643 this_->former_destination=read_former_destinations_from_file();
1644 if (!this_->route || !navit_former_destinations_active(this_))
1646 mr=map_rect_new(this_->former_destination, NULL);
1647 while ((item=map_rect_get_item(mr))) {
1648 if ((item->type == type_former_destination || item->type == type_former_itinerary || item->type == type_former_itinerary_part) && (count=item_coord_get(item, c, 16)))
1651 map_rect_destroy(mr);
1652 if (valid && count > 0) {
1653 for (i = 0 ; i < count ; i++) {
1654 pc[i].pro=map_projection(this_->former_destination);
1659 route_set_destination(this_->route, &pc[0], 1);
1661 route_set_destinations(this_->route, pc, count, 1);
1662 this_->destination=pc[count-1];
1663 this_->destination_valid=1;
1669 navit_textfile_debug_log(struct navit *this_, const char *fmt, ...)
1674 if (this_->textfile_debug_log && this_->vehicle) {
1675 str1=g_strdup_vprintf(fmt, ap);
1676 str2=g_strdup_printf("0x%x 0x%x%s%s\n", this_->vehicle->coord.x, this_->vehicle->coord.y, strlen(str1) ? " " : "", str1);
1677 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1685 navit_textfile_debug_log_at(struct navit *this_, struct pcoord *pc, const char *fmt, ...)
1690 if (this_->textfile_debug_log && this_->vehicle) {
1691 str1=g_strdup_vprintf(fmt, ap);
1692 str2=g_strdup_printf("0x%x 0x%x%s%s\n", pc->x, pc->y, strlen(str1) ? " " : "", str1);
1693 log_write(this_->textfile_debug_log, str2, strlen(str2), 0);
1701 navit_say(struct navit *this_, char *text)
1705 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1707 dbg(1, "this_.speech->active %i\n", attr.u.num);
1709 speech_say(this_->speech, text);
1714 * @brief Toggles the navigation announcer for navit
1715 * @param this_ The navit object
1718 navit_cmd_announcer_toggle(struct navit *this_)
1720 struct attr attr, speechattr;
1722 // search for the speech attribute
1723 if(!navit_get_attr(this_, attr_speech, &speechattr, NULL))
1725 // find out if the corresponding attribute attr_active has been set
1726 if(speech_get_attr(speechattr.u.speech, attr_active, &attr, NULL)) {
1728 attr.u.num = !attr.u.num;
1730 // otherwise disable it because voice is enabled by default
1731 attr.type = attr_active;
1735 // apply the new state
1736 if(!speech_set_attr(speechattr.u.speech, &attr))
1739 // announce that the speech attribute has changed
1740 callback_list_call_attr_0(this_->attr_cbl, attr_speech);
1744 navit_speak(struct navit *this_)
1746 struct navigation *nav=this_->navigation;
1747 struct map *map=NULL;
1748 struct map_rect *mr=NULL;
1752 if (!speech_get_attr(this_->speech, attr_active, &attr, NULL))
1754 dbg(1, "this_.speech->active %i\n", attr.u.num);
1759 map=navigation_get_map(nav);
1761 mr=map_rect_new(map, NULL);
1763 while ((item=map_rect_get_item(mr)) && (item->type == type_nav_position || item->type == type_nav_none));
1764 if (item && item_attr_get(item, attr_navigation_speech, &attr)) {
1765 speech_say(this_->speech, attr.u.str);
1766 navit_add_message(this_, attr.u.str);
1767 navit_textfile_debug_log(this_, "type=announcement label=\"%s\"", attr.u.str);
1769 map_rect_destroy(mr);
1774 navit_window_roadbook_update(struct navit *this_)
1776 struct navigation *nav=this_->navigation;
1777 struct map *map=NULL;
1778 struct map_rect *mr=NULL;
1781 struct param_list param[5];
1785 datawindow_mode(this_->roadbook_window, 1);
1787 map=navigation_get_map(nav);
1789 mr=map_rect_new(map, NULL);
1790 dbg(0,"nav=%p map=%p mr=%p\n", nav, map, mr);
1792 dbg(0,"while loop\n");
1793 while ((item=map_rect_get_item(mr))) {
1794 dbg(0,"item=%p\n", item);
1796 if (item->type != type_nav_position) {
1797 item_attr_get(item, attr_navigation_long, &attr);
1798 if (attr.u.str == NULL) {
1801 dbg(2, "Command='%s'\n", attr.u.str);
1802 param[0].value=g_strdup(attr.u.str);
1804 param[0].value=_("Position");
1805 param[0].name=_("Command");
1807 item_attr_get(item, attr_length, &attr);
1808 dbg(2, "Length=%d\n", attr.u.num);
1809 param[1].name=_("Length");
1811 if ( attr.u.num >= 2000 )
1813 param[1].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1817 param[1].value=g_strdup_printf("%7ld %s",attr.u.num, _("m"));
1820 item_attr_get(item, attr_time, &attr);
1821 dbg(2, "Time=%d\n", attr.u.num);
1823 param[2].name=_("Time");
1826 param[2].value=g_strdup_printf("%d:%02d:%02d",secs / 60, ( secs / 60 ) % 60 , secs % 60);
1830 param[2].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1833 item_attr_get(item, attr_destination_length, &attr);
1834 dbg(2, "Destlength=%d\n", attr.u.num);
1835 param[3].name=_("Destination Length");
1836 if ( attr.u.num >= 2000 )
1838 param[3].value=g_strdup_printf("%5.1f %s",(float)attr.u.num / 1000, _("km") );
1842 param[3].value=g_strdup_printf("%ld %s",attr.u.num, _("m"));
1845 item_attr_get(item, attr_destination_time, &attr);
1846 dbg(2, "Desttime=%d\n", attr.u.num);
1848 param[4].name=_("Destination Time");
1851 param[4].value=g_strdup_printf("%d:%02d:%02d",secs / 3600, (secs / 60 ) % 60 , secs % 60);
1855 param[4].value=g_strdup_printf("%d:%02d",secs / 60, secs % 60);
1857 datawindow_add(this_->roadbook_window, param, 5);
1859 map_rect_destroy(mr);
1861 datawindow_mode(this_->roadbook_window, 0);
1865 navit_window_roadbook_destroy(struct navit *this_)
1868 navigation_unregister_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1869 callback_destroy(this_->roadbook_callback);
1870 this_->roadbook_window=NULL;
1871 this_->roadbook_callback=NULL;
1874 navit_window_roadbook_new(struct navit *this_)
1876 if (!this_->gui || this_->roadbook_callback || this_->roadbook_window) {
1880 this_->roadbook_callback=callback_new_1(callback_cast(navit_window_roadbook_update), this_);
1881 navigation_register_callback(this_->navigation, attr_navigation_long, this_->roadbook_callback);
1882 this_->roadbook_window=gui_datawindow_new(this_->gui, _("Roadbook"), NULL, callback_new_1(callback_cast(navit_window_roadbook_destroy), this_));
1883 navit_window_roadbook_update(this_);
1887 navit_init(struct navit *this_)
1894 dbg(2,"enter gui %p graphics %p\n",this_->gui,this_->gra);
1896 if (!this_->gui && !(this_->flags & 2)) {
1898 navit_destroy(this_);
1901 if (!this_->gra && !(this_->flags & 1)) {
1902 dbg(0,"no graphics\n");
1903 navit_destroy(this_);
1906 dbg(2,"Connecting gui to graphics\n");
1907 if (this_->gui && this_->gra && gui_set_graphics(this_->gui, this_->gra)) {
1908 struct attr attr_type_gui, attr_type_graphics;
1909 gui_get_attr(this_->gui, attr_type, &attr_type_gui, NULL);
1910 graphics_get_attr(this_->gra, attr_type, &attr_type_graphics, NULL);
1911 dbg(0,"failed to connect graphics '%s' to gui '%s'\n", attr_type_graphics.u.str, attr_type_gui.u.str);
1912 dbg(0," Please see http://wiki.navit-project.org/index.php/Failed_to_connect_graphics_to_gui\n");
1913 dbg(0," for explanations and solutions\n");
1915 navit_destroy(this_);
1918 if (this_->speech && this_->navigation) {
1920 speech.type=attr_speech;
1921 speech.u.speech=this_->speech;
1922 navigation_set_attr(this_->navigation, &speech);
1924 dbg(2,"Initializing graphics\n");
1925 dbg(2,"Setting Vehicle\n");
1926 navit_set_vehicle(this_, this_->vehicle);
1927 dbg(2,"Adding dynamic maps to mapset %p\n",this_->mapsets);
1928 if (this_->mapsets) {
1929 struct mapset_handle *msh;
1930 ms=this_->mapsets->data;
1931 this_->progress_cb=callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1932 msh=mapset_open(ms);
1933 while (msh && (map=mapset_next(msh, 0))) {
1934 //pass new callback instance for each map in the mapset to make map callback list destruction work correctly
1935 struct callback *pcb = callback_new_attr_1(callback_cast(navit_map_progress), attr_progress, this_);
1936 map_add_callback(map, pcb);
1941 if ((map=route_get_map(this_->route))) {
1943 map_a.type=attr_map;
1945 mapset_add_attr(ms, &map_a);
1947 if ((map=route_get_graph_map(this_->route))) {
1948 struct attr map_a,active;
1949 map_a.type=attr_map;
1951 active.type=attr_active;
1953 mapset_add_attr(ms, &map_a);
1954 map_set_attr(map, &active);
1956 route_set_mapset(this_->route, ms);
1957 route_set_projection(this_->route, transform_get_projection(this_->trans));
1959 if (this_->tracking) {
1960 tracking_set_mapset(this_->tracking, ms);
1962 tracking_set_route(this_->tracking, this_->route);
1964 if (this_->navigation) {
1965 if ((map=navigation_get_map(this_->navigation))) {
1966 struct attr map_a,active;
1967 map_a.type=attr_map;
1969 active.type=attr_active;
1971 mapset_add_attr(ms, &map_a);
1972 map_set_attr(map, &active);
1975 if (this_->tracking) {
1976 if ((map=tracking_get_map(this_->tracking))) {
1977 struct attr map_a,active;
1978 map_a.type=attr_map;
1980 active.type=attr_active;
1982 mapset_add_attr(ms, &map_a);
1983 map_set_attr(map, &active);
1986 navit_add_former_destinations_from_file(this_);
1989 struct attr callback;
1990 this_->route_cb=callback_new_attr_1(callback_cast(navit_redraw_route), attr_route_status, this_);
1991 callback.type=attr_callback;
1992 callback.u.callback=this_->route_cb;
1993 route_add_attr(this_->route, &callback);
1995 if (this_->navigation) {
1996 if (this_->speech) {
1997 this_->nav_speech_cb=callback_new_1(callback_cast(navit_speak), this_);
1998 navigation_register_callback(this_->navigation, attr_navigation_speech, this_->nav_speech_cb);
2001 navigation_set_route(this_->navigation, this_->route);
2003 dbg(2,"Setting Center\n");
2004 center_file = bookmarks_get_center_file(FALSE);
2005 bookmarks_set_center_from_file(this_->bookmarks, center_file);
2006 g_free(center_file);
2008 if (this_->menubar) {
2009 men=menu_add(this_->menubar, "Data", menu_type_submenu, NULL);
2011 navit_add_menu_windows_items(this_, men);
2017 navit_window_roadbook_new(this_);
2018 navit_window_items_new(this_);
2021 messagelist_init(this_->messages);
2023 navit_set_cursors(this_);
2025 callback_list_call_attr_1(this_->attr_cbl, attr_navit, this_);
2026 callback=(this_->ready == 2);
2028 dbg(2,"ready=%d\n",this_->ready);
2029 if (this_->ready == 3)
2030 navit_draw_async(this_, 1);
2032 callback_list_call_attr_1(this_->attr_cbl, attr_graphics_ready, this_);
2034 routech_test(this_);
2039 navit_zoom_to_rect(struct navit *this_, struct coord_rect *r)
2044 c.x=(r->rl.x+r->lu.x)/2;
2045 c.y=(r->rl.y+r->lu.y)/2;
2046 transform_set_center(this_->trans, &c);
2047 transform_get_size(this_->trans, &w, &h);
2048 dbg(0,"center 0x%x,0x%x w %d h %d\n",c.x,c.y,w,h);
2049 dbg(0,"%x,%x-%x,%x\n", r->lu.x,r->lu.y,r->rl.x,r->rl.y);
2050 while (scale < 1<<20) {
2052 transform_set_scale(this_->trans, scale);
2053 transform_setup_source_rect(this_->trans);
2054 transform(this_->trans, transform_get_projection(this_->trans), &r->lu, &p1, 1, 0, 0, NULL);
2055 transform(this_->trans, transform_get_projection(this_->trans), &r->rl, &p2, 1, 0, 0, NULL);
2056 dbg(0,"%d,%d-%d,%d\n",p1.x,p1.y,p2.x,p2.y);
2057 if (p1.x < 0 || p2.x < 0 || p1.x > w || p2.x > w ||
2058 p1.y < 0 || p2.y < 0 || p1.y > h || p2.y > h)
2064 dbg(0,"scale=%d (0x%x) of %d (0x%x)\n",scale,scale,1<<20,1<<20);
2065 if (this_->ready == 3)
2066 navit_draw_async(this_,0);
2070 navit_zoom_to_route(struct navit *this_, int orientation)
2073 struct map_rect *mr=NULL;
2076 struct coord_rect r;
2081 map=route_get_map(this_->route);
2082 dbg(1,"map=%p\n",map);
2084 mr=map_rect_new(map, NULL);
2085 dbg(1,"mr=%p\n",mr);
2087 while ((item=map_rect_get_item(mr))) {
2088 dbg(1,"item=%s\n", item_to_name(item->type));
2089 while (item_coord_get(item, &c, 1)) {
2094 coord_rect_extend(&r, &c);
2098 map_rect_destroy(mr);
2102 if (orientation != -1)
2103 transform_set_yaw(this_->trans, orientation);
2104 navit_zoom_to_rect(this_, &r);
2108 navit_cmd_zoom_to_route(struct navit *this)
2110 navit_zoom_to_route(this, 0);
2115 * Change the current zoom level
2117 * @param navit The navit instance
2118 * @param center The point where to center the map, including its projection
2122 navit_set_center(struct navit *this_, struct pcoord *center, int set_timeout)
2124 struct coord *c=transform_center(this_->trans);
2126 enum projection pro = transform_get_projection(this_->trans);
2127 if (pro != center->pro) {
2130 transform_from_to(&c1, center->pro, &c2, pro);
2137 navit_set_timeout(this_);
2138 if (this_->ready == 3)
2143 navit_set_center_coord_screen(struct navit *this_, struct coord *c, struct point *p, int set_timeout)
2147 transform_set_center(this_->trans, c);
2148 transform_get_size(this_->trans, &width, &height);
2151 update_transformation(this_->trans, &po, p, NULL);
2153 navit_set_timeout(this_);
2157 * Links all vehicles to a cursor depending on the current profile.
2159 * @param this_ A navit instance
2160 * @author Ralph Sennhauser (10/2009)
2163 navit_set_cursors(struct navit *this_)
2166 struct navit_vehicle *nv;
2170 v=g_list_first(this_->vehicles); // GList of navit_vehicles
2173 if (vehicle_get_attr(nv->vehicle, attr_cursorname, &name, NULL)) {
2174 if (!strcmp(name.u.str,"none"))
2177 c=layout_get_cursor(this_->layout_current, name.u.str);
2179 c=layout_get_cursor(this_->layout_current, "default");
2180 vehicle_set_cursor(nv->vehicle, c, 0);
2187 navit_get_cursor_pnt(struct navit *this_, struct point *p, int keep_orientation, int *dir)
2190 struct navit_vehicle *nv=this_->vehicle;
2192 float offset=this_->radius; // Cursor offset from the center of the screen (percent).
2193 #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 */
2194 float min_offset = 0.; // Percent offset at min_offset_speed.
2195 float max_offset = 30.; // Percent offset at max_offset_speed.
2196 int min_offset_speed = 2; // Speed in km/h
2197 int max_offset_speed = 50; // Speed ini km/h
2198 // Calculate cursor offset from the center of the screen, upon speed.
2199 if (nv->speed <= min_offset_speed) {
2200 offset = min_offset;
2201 } else if (nv->speed > max_offset_speed) {
2202 offset = max_offset;
2204 offset = (max_offset - min_offset) / (max_offset_speed - min_offset_speed) * (nv->speed - min_offset_speed);
2208 transform_get_size(this_->trans, &width, &height);
2209 if (this_->orientation == -1 || keep_orientation) {
2211 p->y=(50 + offset)*height/100;
2213 *dir=keep_orientation?this_->orientation:nv->dir;
2216 if (this_->tracking && this_->tracking_flag) {
2217 mdir = tracking_get_angle(this_->tracking) - this_->orientation;
2219 mdir=nv->dir-this_->orientation;
2222 p->x=(50 - offset*sin(M_PI*mdir/180.))*width/100;
2223 p->y=(50 + offset*cos(M_PI*mdir/180.))*height/100;
2225 *dir=this_->orientation;
2231 navit_set_center_cursor(struct navit *this_, int autozoom, int keep_orientation)
2235 struct navit_vehicle *nv=this_->vehicle;
2236 navit_get_cursor_pnt(this_, &pn, keep_orientation, &dir);
2237 transform_set_yaw(this_->trans, dir);
2238 navit_set_center_coord_screen(this_, &nv->coord, &pn, 0);
2240 navit_autozoom(this_, &nv->coord, nv->speed, 0);
2244 navit_set_center_cursor_draw(struct navit *this_)
2246 navit_set_center_cursor(this_,1,0);
2247 if (this_->ready == 3)
2248 navit_draw_async(this_, 1);
2252 navit_cmd_set_center_cursor(struct navit *this_)
2254 navit_set_center_cursor_draw(this_);
2258 navit_set_center_screen(struct navit *this_, struct point *p, int set_timeout)
2262 transform_reverse(this_->trans, p, &c);
2265 pc.pro = transform_get_projection(this_->trans);
2266 navit_set_center(this_, &pc, set_timeout);
2270 navit_set_attr_do(struct navit *this_, struct attr *attr, int init)
2272 int dir=0, orient_old=0, attr_updated=0;
2276 struct navit_vehicle *nv;
2279 active.type=attr_active;
2282 switch (attr->type) {
2284 attr_updated=(this_->autozoom_secs != attr->u.num);
2285 this_->autozoom_secs = attr->u.num;
2287 case attr_autozoom_active:
2288 attr_updated=(this_->autozoom_active != attr->u.num);
2289 this_->autozoom_active = attr->u.num;
2292 transform_from_geo(transform_get_projection(this_->trans), attr->u.coord_geo, &co);
2293 dbg(1,"0x%x,0x%x\n",co.x,co.y);
2294 transform_set_center(this_->trans, &co);
2296 case attr_drag_bitmap:
2297 attr_updated=(this_->drag_bitmap != !!attr->u.num);
2298 this_->drag_bitmap=!!attr->u.num;
2301 attr_updated=(this_->flags != attr->u.num);
2302 this_->flags=attr->u.num;
2304 case attr_flags_graphics:
2305 attr_updated=(this_->graphics_flags != attr->u.num);
2306 this_->graphics_flags=attr->u.num;
2309 if (!this_->vehicle)
2311 attr_updated=(this_->vehicle->follow_curr != attr->u.num);
2312 this_->vehicle->follow_curr = attr->u.num;
2315 if(this_->layout_current!=attr->u.layout) {
2316 this_->layout_current=attr->u.layout;
2317 graphics_font_destroy_all(this_->gra);
2318 navit_set_cursors(this_);
2319 if (this_->ready == 3)
2324 case attr_layout_name:
2328 if (!strcmp(lay->name,attr->u.str)) {
2330 attr.type=attr_layout;
2332 return navit_set_attr_do(this_, &attr, init);
2337 case attr_map_border:
2338 if (this_->border != attr->u.num) {
2339 this_->border=attr->u.num;
2343 case attr_orientation:
2344 orient_old=this_->orientation;
2345 this_->orientation=attr->u.num;
2347 if (this_->orientation != -1) {
2348 dir = this_->orientation;
2350 if (this_->vehicle) {
2351 dir = this_->vehicle->dir;
2354 transform_set_yaw(this_->trans, dir);
2355 if (orient_old != this_->orientation) {
2357 if (this_->ready == 3)
2364 case attr_osd_configuration:
2365 dbg(0,"setting osd_configuration to %d (was %d)\n", attr->u.num, this_->osd_configuration);
2366 attr_updated=(this_->osd_configuration != attr->u.num);
2367 this_->osd_configuration=attr->u.num;
2370 attr_updated=(this_->pitch != attr->u.num);
2371 this_->pitch=attr->u.num;
2372 transform_set_pitch(this_->trans, this_->pitch);
2373 if (!init && attr_updated && this_->ready == 3)
2376 case attr_projection:
2377 if(this_->trans && transform_get_projection(this_->trans) != attr->u.projection) {
2378 navit_projection_set(this_, attr->u.projection, !init);
2383 attr_updated=(this_->radius != attr->u.num);
2384 this_->radius=attr->u.num;
2386 case attr_recent_dest:
2387 attr_updated=(this_->recentdest_count != attr->u.num);
2388 this_->recentdest_count=attr->u.num;
2391 if(this_->speech && this_->speech != attr->u.speech) {
2393 this_->speech = attr->u.speech;
2397 attr_updated=(this_->center_timeout != attr->u.num);
2398 this_->center_timeout = attr->u.num;
2401 attr_updated=(this_->tracking_flag != !!attr->u.num);
2402 this_->tracking_flag=!!attr->u.num;
2404 case attr_transformation:
2405 this_->trans=attr->u.transformation;
2407 case attr_use_mousewheel:
2408 attr_updated=(this_->use_mousewheel != !!attr->u.num);
2409 this_->use_mousewheel=!!attr->u.num;
2415 if (nv->vehicle == attr->u.vehicle) {
2416 if (!this_->vehicle || this_->vehicle->vehicle != attr->u.vehicle) {
2418 vehicle_set_attr(this_->vehicle->vehicle, &active);
2420 vehicle_set_attr(nv->vehicle, &active);
2423 navit_set_vehicle(this_, nv);
2429 zoom=transform_get_scale(this_->trans);
2430 attr_updated=(zoom != attr->u.num);
2431 transform_set_scale(this_->trans, attr->u.num);
2432 if (attr_updated && !init)
2436 attr_updated=(attr->u.num != this_->zoom_min);
2437 this_->zoom_min=attr->u.num;
2440 attr_updated=(attr->u.num != this_->zoom_max);
2441 this_->zoom_max=attr->u.num;
2444 navit_add_message(this_, attr->u.str);
2446 case attr_follow_cursor:
2447 attr_updated=(this_->follow_cursor != !!attr->u.num);
2448 this_->follow_cursor=!!attr->u.num;
2451 attr_updated=(this_->imperial != attr->u.num);
2452 this_->imperial=attr->u.num;
2457 if (attr_updated && !init) {
2458 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2459 if (attr->type == attr_osd_configuration)
2460 graphics_draw_mode(this_->gra, draw_mode_end);
2466 navit_set_attr(struct navit *this_, struct attr *attr)
2468 return navit_set_attr_do(this_, attr, 0);
2472 navit_get_attr(struct navit *this_, enum attr_type type, struct attr *attr, struct attr_iter *iter)
2474 struct message *msg;
2480 msg = navit_get_messages(this_);
2488 len += strlen(msg->text) + 1;
2491 attr->u.str = g_malloc(len + 1);
2493 msg = navit_get_messages(this_);
2496 g_stpcpy((attr->u.str + offset), msg->text);
2497 offset += strlen(msg->text);
2498 attr->u.str[offset] = '\n';
2504 attr->u.str[len] = '\0';
2507 attr->u.num=this_->imperial;
2509 case attr_bookmark_map:
2510 attr->u.map=bookmarks_get_map(this_->bookmarks);
2512 case attr_bookmarks:
2513 attr->u.bookmarks=this_->bookmarks;
2515 case attr_callback_list:
2516 attr->u.callback_list=this_->attr_cbl;
2518 case attr_destination:
2519 if (! this_->destination_valid)
2521 attr->u.pcoord=&this_->destination;
2523 case attr_displaylist:
2524 attr->u.displaylist=this_->displaylist;
2525 return (attr->u.displaylist != NULL);
2527 if (!this_->vehicle)
2529 attr->u.num=this_->vehicle->follow_curr;
2531 case attr_former_destination_map:
2532 attr->u.map=this_->former_destination;
2535 attr->u.graphics=this_->gra;
2536 ret=(attr->u.graphics != NULL);
2539 attr->u.gui=this_->gui;
2540 ret=(attr->u.gui != NULL);
2545 iter->u.list=g_list_next(iter->u.list);
2547 iter->u.list=this_->layouts;
2551 attr->u.layout=(struct layout *)iter->u.list->data;
2553 attr->u.layout=this_->layout_current;
2557 if (iter && this_->mapsets) {
2558 if (!iter->u.mapset_handle) {
2559 iter->u.mapset_handle=mapset_open((struct mapset *)this_->mapsets->data);
2561 attr->u.map=mapset_next(iter->u.mapset_handle, 0);
2563 mapset_close(iter->u.mapset_handle);
2571 attr->u.mapset=this_->mapsets->data;
2572 ret=(attr->u.mapset != NULL);
2574 case attr_navigation:
2575 attr->u.navigation=this_->navigation;
2577 case attr_orientation:
2578 attr->u.num=this_->orientation;
2581 ret=attr_generic_get_attr(this_->attr_list, NULL, type, attr, iter?(struct attr_iter *)&iter->iter:NULL);
2583 case attr_osd_configuration:
2584 attr->u.num=this_->osd_configuration;
2587 attr->u.num=transform_get_pitch(this_->trans);
2589 case attr_projection:
2591 attr->u.num=transform_get_projection(this_->trans);
2597 attr->u.route=this_->route;
2600 attr->u.speech=this_->speech;
2603 attr->u.num=this_->tracking_flag;
2605 case attr_trackingo:
2606 attr->u.tracking=this_->tracking;
2608 case attr_transformation:
2609 attr->u.transformation=this_->trans;
2614 iter->u.list=g_list_next(iter->u.list);
2616 iter->u.list=this_->vehicles;
2620 attr->u.vehicle=((struct navit_vehicle*)iter->u.list->data)->vehicle;
2622 if(this_->vehicle) {
2623 attr->u.vehicle=this_->vehicle->vehicle;
2629 case attr_vehicleprofile:
2630 attr->u.vehicleprofile=this_->vehicleprofile;
2633 attr->u.num=transform_get_scale(this_->trans);
2635 case attr_autozoom_active:
2636 attr->u.num=this_->autozoom_active;
2638 case attr_follow_cursor:
2639 attr->u.num=this_->follow_cursor;
2649 navit_add_log(struct navit *this_, struct log *log)
2651 struct attr type_attr;
2652 if (!log_get_attr(log, attr_type, &type_attr, NULL))
2654 if (!strcmp(type_attr.u.str, "textfile_debug")) {
2655 char *header = "type=track_tracked\n";
2656 if (this_->textfile_debug_log)
2658 log_set_header(log, header, strlen(header));
2659 this_->textfile_debug_log=log;
2666 navit_add_layout(struct navit *this_, struct layout *layout)
2669 this_->layouts = g_list_append(this_->layouts, layout);
2670 layout_get_attr(layout, attr_active, &active, NULL);
2671 if(active.u.num || !this_->layout_current) {
2672 this_->layout_current=layout;
2679 navit_add_attr(struct navit *this_, struct attr *attr)
2682 switch (attr->type) {
2684 navit_add_callback(this_, attr->u.callback);
2687 ret=navit_add_log(this_, attr->u.log);
2690 ret=navit_set_gui(this_, attr->u.gui);
2693 ret=navit_set_graphics(this_, attr->u.graphics);
2696 ret=navit_add_layout(this_, attr->u.layout);
2699 this_->route=attr->u.route;
2702 this_->mapsets = g_list_append(this_->mapsets, attr->u.mapset);
2704 case attr_navigation:
2705 this_->navigation=attr->u.navigation;
2708 this_->attr_list=attr_generic_add_attr(this_->attr_list, attr);
2710 case attr_recent_dest:
2711 this_->recentdest_count = attr->u.num;
2714 this_->speech=attr->u.speech;
2716 case attr_trackingo:
2717 this_->tracking=attr->u.tracking;
2720 ret=navit_add_vehicle(this_, attr->u.vehicle);
2722 case attr_vehicleprofile:
2723 this_->vehicleprofiles=g_list_prepend(this_->vehicleprofiles, attr->u.vehicleprofile);
2725 case attr_autozoom_min:
2726 this_->autozoom_min = attr->u.num;
2732 this_->attrs=attr_generic_add_attr(this_->attrs, attr);
2733 callback_list_call_attr_2(this_->attr_cbl, attr->type, this_, attr);
2738 navit_remove_attr(struct navit *this_, struct attr *attr)
2741 switch (attr->type) {
2743 navit_remove_callback(this_, attr->u.callback);
2746 this_->attrs=attr_generic_remove_attr(this_->attrs, attr);
2755 navit_attr_iter_new(void)
2757 return g_new0(struct attr_iter, 1);
2761 navit_attr_iter_destroy(struct attr_iter *iter)
2767 navit_add_callback(struct navit *this_, struct callback *cb)
2769 callback_list_add(this_->attr_cbl, cb);
2773 navit_remove_callback(struct navit *this_, struct callback *cb)
2775 callback_list_remove(this_->attr_cbl, cb);
2779 * Toggle the cursor update : refresh the map each time the cursor has moved (instead of only when it reaches a border)
2781 * @param navit The navit instance
2786 navit_vehicle_draw(struct navit *this_, struct navit_vehicle *nv, struct point *pnt)
2788 struct point cursor_pnt;
2789 enum projection pro;
2796 pro=transform_get_projection(this_->trans_cursor);
2799 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2801 vehicle_draw(nv->vehicle, this_->gra, &cursor_pnt, pnt ? 0:1, nv->dir-transform_get_yaw(this_->trans_cursor), nv->speed);
2806 pro=transform_get_projection(this_->trans);
2807 transform(this_->trans, pro, &nv->coord, &pnt2, 1);
2810 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, pnt == NULL);
2812 cursor_draw(nv->cursor, &pnt2, nv->dir-transform_get_angle(this_->trans, 0), nv->speed > 2, 1);
2818 navit_vehicle_update(struct navit *this_, struct navit_vehicle *nv)
2820 struct attr attr_valid, attr_dir, attr_speed, attr_pos;
2821 struct pcoord cursor_pc;
2822 struct point cursor_pnt, *pnt=&cursor_pnt;
2823 struct tracking *tracking=NULL;
2824 struct pcoord pc[16];
2825 enum projection pro=transform_get_projection(this_->trans_cursor);
2827 int (*get_attr)(void *, enum attr_type, struct attr *, struct attr_iter *);
2829 char *destination_file;
2832 if (this_->ready != 3) {
2833 profile(0,"return 1\n");
2836 navit_layout_switch(this_);
2837 if (this_->vehicle == nv && this_->tracking_flag)
2838 tracking=this_->tracking;
2840 tracking_update(tracking, nv->vehicle, this_->vehicleprofile, pro);
2841 attr_object=tracking;
2842 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))tracking_get_attr;
2844 attr_object=nv->vehicle;
2845 get_attr=(int (*)(void *, enum attr_type, struct attr *, struct attr_iter *))vehicle_get_attr;
2847 if (get_attr(attr_object, attr_position_valid, &attr_valid, NULL))
2848 if (!attr_valid.u.num != attr_position_valid_invalid)
2850 if (! get_attr(attr_object, attr_position_direction, &attr_dir, NULL) ||
2851 ! get_attr(attr_object, attr_position_speed, &attr_speed, NULL) ||
2852 ! get_attr(attr_object, attr_position_coord_geo, &attr_pos, NULL)) {
2853 profile(0,"return 2\n");
2856 nv->dir=*attr_dir.u.numd;
2857 nv->speed=*attr_speed.u.numd;
2858 transform_from_geo(pro, attr_pos.u.coord_geo, &nv->coord);
2859 if (nv != this_->vehicle) {
2860 navit_vehicle_draw(this_, nv, NULL);
2861 profile(0,"return 3\n");
2864 cursor_pc.x = nv->coord.x;
2865 cursor_pc.y = nv->coord.y;
2866 cursor_pc.pro = pro;
2869 route_set_position_from_tracking(this_->route, tracking, pro);
2871 route_set_position(this_->route, &cursor_pc);
2873 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2874 navit_textfile_debug_log(this_, "type=trackpoint_tracked");
2875 if (this_->gui && nv->speed > 2)
2876 navit_disable_suspend();
2878 transform(this_->trans_cursor, pro, &nv->coord, &cursor_pnt, 1, 0, 0, NULL);
2879 if (this_->button_pressed != 1 && this_->follow_cursor && nv->follow_curr <= nv->follow &&
2880 (nv->follow_curr == 1 || !transform_within_border(this_->trans_cursor, &cursor_pnt, this_->border)))
2881 navit_set_center_cursor_draw(this_);
2883 navit_vehicle_draw(this_, nv, pnt);
2885 if (nv->follow_curr > 1)
2888 nv->follow_curr=nv->follow;
2889 callback_list_call_attr_2(this_->attr_cbl, attr_position_coord_geo, this_, nv->vehicle);
2891 /* Finally, if we reached our destination, stop navigation. */
2893 switch(route_destination_reached(this_->route)) {
2895 route_remove_waypoint(this_->route);
2896 count=route_get_destinations(this_->route, pc, 16);
2897 destination_file = bookmarks_get_destination_file(TRUE);
2898 bookmarks_append_coord(this_->former_destination, destination_file, pc, type_former_itinerary_part, NULL, this_->recentdest_count);
2901 navit_set_destination(this_, NULL, NULL, 0);
2905 profile(0,"return 5\n");
2909 * Set the position of the vehicle
2911 * @param navit The navit instance
2912 * @param c The coordinate to set as position
2917 navit_set_position(struct navit *this_, struct pcoord *c)
2920 route_set_position(this_->route, c);
2921 callback_list_call_attr_0(this_->attr_cbl, attr_position);
2923 if (this_->ready == 3)
2928 navit_set_vehicleprofile(struct navit *this_, char *name)
2932 l=this_->vehicleprofiles;
2934 if (vehicleprofile_get_attr(l->data, attr_name, &attr, NULL)) {
2935 if (!strcmp(attr.u.str, name)) {
2936 this_->vehicleprofile=l->data;
2938 route_set_profile(this_->route, this_->vehicleprofile);
2948 navit_set_vehicle(struct navit *this_, struct navit_vehicle *nv)
2952 if (nv && vehicle_get_attr(nv->vehicle, attr_profilename, &attr, NULL)) {
2953 if (navit_set_vehicleprofile(this_, attr.u.str))
2956 if (!navit_set_vehicleprofile(this_,"car")) {
2957 /* We do not have a fallback "car" profile
2958 * so lets set any profile */
2960 l=this_->vehicleprofiles;
2962 this_->vehicleprofile=l->data;
2964 route_set_profile(this_->route, this_->vehicleprofile);
2970 * Register a new vehicle
2972 * @param navit The navit instance
2973 * @param v The vehicle instance
2974 * @returns 1 for success
2977 navit_add_vehicle(struct navit *this_, struct vehicle *v)
2979 struct navit_vehicle *nv=g_new0(struct navit_vehicle, 1);
2980 struct attr follow, active, animate;
2985 nv->animate_cursor=0;
2986 if ((vehicle_get_attr(v, attr_follow, &follow, NULL)))
2987 nv->follow=follow.u.num;
2988 nv->follow_curr=nv->follow;
2989 this_->vehicles=g_list_append(this_->vehicles, nv);
2990 if ((vehicle_get_attr(v, attr_active, &active, NULL)) && active.u.num)
2991 navit_set_vehicle(this_, nv);
2992 if ((vehicle_get_attr(v, attr_animate, &animate, NULL)))
2993 nv->animate_cursor=animate.u.num;
2994 nv->callback.type=attr_callback;
2995 nv->callback.u.callback=callback_new_attr_2(callback_cast(navit_vehicle_update), attr_position_coord_geo, this_, nv);
2996 vehicle_add_attr(nv->vehicle, &nv->callback);
2997 vehicle_set_attr(nv->vehicle, &this_->self);
3005 navit_get_gui(struct navit *this_)
3010 struct transformation *
3011 navit_get_trans(struct navit *this_)
3013 return this_->trans;
3017 navit_get_route(struct navit *this_)
3019 return this_->route;
3023 navit_get_navigation(struct navit *this_)
3025 return this_->navigation;
3028 struct displaylist *
3029 navit_get_displaylist(struct navit *this_)
3031 return this_->displaylist;
3035 navit_layout_switch(struct navit *n)
3039 struct attr iso8601_attr,geo_attr,valid_attr,layout_attr;
3040 double trise,tset,trise_actual;
3042 int year, month, day;
3044 if (navit_get_attr(n,attr_layout,&layout_attr,NULL)!=1) {
3045 return; //No layout - nothing to switch
3049 l=layout_attr.u.layout;
3051 if (l->dayname || l->nightname) {
3052 //Ok, we know that we have profile to switch
3054 //Check that we aren't calculating too fast
3055 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_time_iso8601,&iso8601_attr,NULL)==1) {
3056 currTs=iso8601_to_secs(iso8601_attr.u.str);
3057 dbg(1,"currTs: %u:%u\n",currTs%86400/3600,((currTs%86400)%3600)/60);
3059 if (currTs-(n->prevTs)<60) {
3060 //We've have to wait a little
3063 if (sscanf(iso8601_attr.u.str,"%d-%02d-%02dT",&year,&month,&day) != 3)
3065 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_valid, &valid_attr,NULL) && valid_attr.u.num==attr_position_valid_invalid) {
3066 return; //No valid fix yet
3068 if (vehicle_get_attr(n->vehicle->vehicle, attr_position_coord_geo,&geo_attr,NULL)!=1) {
3069 //No position - no sun
3073 //We calculate sunrise anyway, cause it is needed both for day and for night
3074 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3075 //near the pole sun never rises/sets, so we should never switch profiles
3076 dbg(1,"trise: %u:%u, sun never visible, never switch profile\n",HOURS(trise),MINUTES(trise));
3082 dbg(1,"trise: %u:%u\n",HOURS(trise),MINUTES(trise));
3085 if ((HOURS(trise)*60+MINUTES(trise)==(currTs%86400)/60) ||
3086 (n->prevTs==0 && ((HOURS(trise)*60+MINUTES(trise)<(currTs%86400)/60)))) {
3087 //The sun is rising now!
3088 if (strcmp(l->name,l->dayname)) {
3089 navit_set_layout_by_name(n,l->dayname);
3094 if (__sunriset__(year,month,day,geo_attr.u.coord_geo->lng,geo_attr.u.coord_geo->lat,-5,1,&trise,&tset)!=0) {
3095 //near the pole sun never rises/sets, so we should never switch profiles
3096 dbg(1,"tset: %u:%u, sun always visible, never switch profile\n",HOURS(tset),MINUTES(tset));
3100 dbg(1,"tset: %u:%u\n",HOURS(tset),MINUTES(tset));
3101 if (HOURS(tset)*60+MINUTES(tset)==((currTs%86400)/60)
3102 || (n->prevTs==0 && (((HOURS(tset)*60+MINUTES(tset)<(currTs%86400)/60)) ||
3103 ((HOURS(trise_actual)*60+MINUTES(trise_actual)>(currTs%86400)/60))))) {
3105 if (strcmp(l->name,l->nightname)) {
3106 navit_set_layout_by_name(n,l->nightname);
3116 navit_set_vehicle_by_name(struct navit *n,const char *name)
3119 struct attr_iter *iter;
3120 struct attr vehicle_attr, name_attr;
3122 iter=navit_attr_iter_new();
3124 while (navit_get_attr(n,attr_vehicle,&vehicle_attr,iter)) {
3125 v=vehicle_attr.u.vehicle;
3126 vehicle_get_attr(v,attr_name,&name_attr,NULL);
3127 if (name_attr.type==attr_name) {
3128 if (!strcmp(name,name_attr.u.str)) {
3129 navit_set_attr(n,&vehicle_attr);
3130 navit_attr_iter_destroy(iter);
3135 navit_attr_iter_destroy(iter);
3140 navit_set_layout_by_name(struct navit *n,const char *name)
3143 struct attr_iter iter;
3144 struct attr layout_attr;
3148 if (navit_get_attr(n,attr_layout,&layout_attr,&iter)!=1) {
3149 return 0; //No layouts - nothing to do
3151 if (iter.u.list==NULL) {
3155 iter.u.list=g_list_first(iter.u.list);
3157 while(iter.u.list) {
3158 l=(struct layout*)iter.u.list->data;
3159 if (!strcmp(name,l->name)) {
3160 layout_attr.u.layout=l;
3161 layout_attr.type=attr_layout;
3162 navit_set_attr(n,&layout_attr);
3163 iter.u.list=g_list_first(iter.u.list);
3166 iter.u.list=g_list_next(iter.u.list);
3169 iter.u.list=g_list_first(iter.u.list);
3174 navit_disable_suspend() {
3175 gui_disable_suspend(global_navit->gui);
3176 callback_list_call_attr_0(global_navit->attr_cbl,attr_unsuspend);
3180 navit_block(struct navit *this_, int block)
3183 this_->blocked |= 1;
3184 if (graphics_draw_cancel(this_->gra, this_->displaylist))
3185 this_->blocked |= 2;
3188 if ((this_->blocked & 2) || block < 0) {
3198 navit_destroy(struct navit *this_)
3200 dbg(0,"enter %p\n",this_);
3201 graphics_draw_cancel(this_->gra, this_->displaylist);
3202 callback_list_call_attr_1(this_->attr_cbl, attr_destroy, this_);
3203 attr_list_free(this_->attrs);
3204 if (this_->bookmarks) {
3205 char *center_file = bookmarks_get_center_file(TRUE);
3206 bookmarks_write_center_to_file(this_->bookmarks, center_file);
3207 g_free(center_file);
3208 bookmarks_destroy(this_->bookmarks);
3211 callback_destroy(this_->nav_speech_cb);
3212 callback_destroy(this_->roadbook_callback);
3213 callback_destroy(this_->popup_callback);
3214 callback_destroy(this_->motion_timeout_callback);
3215 callback_destroy(this_->progress_cb);
3218 graphics_remove_callback(this_->gra, this_->resize_callback);
3219 graphics_remove_callback(this_->gra, this_->button_callback);
3220 graphics_remove_callback(this_->gra, this_->motion_callback);
3221 graphics_remove_callback(this_->gra, this_->predraw_callback);
3224 callback_destroy(this_->resize_callback);
3225 callback_destroy(this_->motion_callback);
3226 callback_destroy(this_->predraw_callback);
3228 callback_destroy(this_->route_cb);
3230 route_destroy(this_->route);
3232 map_destroy(this_->former_destination);
3234 graphics_displaylist_destroy(this_->displaylist);
3236 graphics_free(this_->gra);
3242 navit_ref(struct navit *this_)
3245 dbg(0,"refcount %d\n",this_->refcount);
3250 navit_unref(struct navit *this_)
3253 dbg(0,"refcount %d\n",this_->refcount);
3254 if (this_->refcount <= 0)
3255 navit_destroy(this_);
3258 struct object_func navit_func = {
3260 (object_func_new)navit_new,
3261 (object_func_get_attr)navit_get_attr,
3262 (object_func_iter_new)navit_attr_iter_new,
3263 (object_func_iter_destroy)navit_attr_iter_destroy,
3264 (object_func_set_attr)navit_set_attr,
3265 (object_func_add_attr)navit_add_attr,
3266 (object_func_remove_attr)navit_remove_attr,
3267 (object_func_init)navit_init,
3268 (object_func_destroy)navit_destroy,
3269 (object_func_dup)NULL,
3270 (object_func_ref)navit_ref,
3271 (object_func_unref)navit_unref,